add custom system prompt (#39)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { Conversation, OpenAIModelID, OpenAIModels } from "@/types";
|
||||
import { DEFAULT_SYSTEM_PROMPT } from "./const";
|
||||
|
||||
export const cleanSelectedConversation = (conversation: Conversation) => {
|
||||
// added model for each conversation (3/20/23)
|
||||
// added system prompt for each conversation (3/21/23)
|
||||
|
||||
let updatedConversation = conversation;
|
||||
|
||||
// check for model on each conversation
|
||||
if (!updatedConversation.model) {
|
||||
updatedConversation = {
|
||||
...updatedConversation,
|
||||
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||
};
|
||||
}
|
||||
|
||||
// check for system prompt on each conversation
|
||||
if (!updatedConversation.prompt) {
|
||||
updatedConversation = {
|
||||
...updatedConversation,
|
||||
prompt: DEFAULT_SYSTEM_PROMPT
|
||||
};
|
||||
}
|
||||
|
||||
return updatedConversation;
|
||||
};
|
||||
|
||||
export const cleanConversationHistory = (history: Conversation[]) => {
|
||||
// added model for each conversation (3/20/23)
|
||||
// added system prompt for each conversation (3/21/23)
|
||||
|
||||
let updatedHistory = [...history];
|
||||
|
||||
// check for model on each conversation
|
||||
if (!updatedHistory.every((conversation) => conversation.model)) {
|
||||
updatedHistory = updatedHistory.map((conversation) => ({
|
||||
...conversation,
|
||||
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||
}));
|
||||
}
|
||||
|
||||
// check for system prompt on each conversation
|
||||
if (!updatedHistory.every((conversation) => conversation.prompt)) {
|
||||
updatedHistory = updatedHistory.map((conversation) => ({
|
||||
...conversation,
|
||||
systemPrompt: DEFAULT_SYSTEM_PROMPT
|
||||
}));
|
||||
}
|
||||
|
||||
return updatedHistory;
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export const DEFAULT_SYSTEM_PROMPT = "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.";
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Conversation } from "@/types";
|
||||
|
||||
export const updateConversation = (updatedConversation: Conversation, allConversations: Conversation[]) => {
|
||||
const updatedConversations = allConversations.map((c) => {
|
||||
if (c.id === updatedConversation.id) {
|
||||
return updatedConversation;
|
||||
}
|
||||
|
||||
return c;
|
||||
});
|
||||
|
||||
saveConversation(updatedConversation);
|
||||
saveConversations(updatedConversations);
|
||||
|
||||
return {
|
||||
single: updatedConversation,
|
||||
all: updatedConversations
|
||||
};
|
||||
};
|
||||
|
||||
export const saveConversation = (conversation: Conversation) => {
|
||||
localStorage.setItem("selectedConversation", JSON.stringify(conversation));
|
||||
};
|
||||
|
||||
export const saveConversations = (conversations: Conversation[]) => {
|
||||
localStorage.setItem("conversationHistory", JSON.stringify(conversations));
|
||||
};
|
||||
@@ -1,33 +0,0 @@
|
||||
import { Conversation, OpenAIModelID, OpenAIModels } from "@/types";
|
||||
|
||||
export const cleanConversationHistory = (history: Conversation[]) => {
|
||||
// added model for each conversation (3/20/23)
|
||||
|
||||
if (history.length === 0) {
|
||||
return history;
|
||||
} else {
|
||||
return history.map((conversation) => {
|
||||
if (conversation.model) {
|
||||
return conversation;
|
||||
} else {
|
||||
return {
|
||||
...conversation,
|
||||
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const cleanSelectedConversation = (conversation: Conversation) => {
|
||||
// added model for each conversation (3/20/23)
|
||||
|
||||
if (conversation.model) {
|
||||
return conversation;
|
||||
} else {
|
||||
return {
|
||||
...conversation,
|
||||
model: OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -1,10 +1,7 @@
|
||||
import { Message, OpenAIModel } from "@/types";
|
||||
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
|
||||
|
||||
export const OpenAIStream = async (model: OpenAIModel, key: string, messages: Message[]) => {
|
||||
const encoder = new TextEncoder();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
export const OpenAIStream = async (model: OpenAIModel, systemPrompt: string, key: string, messages: Message[]) => {
|
||||
const res = await fetch("https://api.openai.com/v1/chat/completions", {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -16,7 +13,7 @@ export const OpenAIStream = async (model: OpenAIModel, key: string, messages: Me
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: `You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown format.`
|
||||
content: systemPrompt
|
||||
},
|
||||
...messages
|
||||
],
|
||||
@@ -30,6 +27,9 @@ export const OpenAIStream = async (model: OpenAIModel, key: string, messages: Me
|
||||
throw new Error("OpenAI API returned an error");
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
const onParse = (event: ParsedEvent | ReconnectInterval) => {
|
||||
|
||||
Reference in New Issue
Block a user