feat: add in prettier and format code for consistency (#168)
This commit is contained in:
+9
-6
@@ -1,5 +1,5 @@
|
||||
import { Conversation, OpenAIModelID, OpenAIModels } from "@/types";
|
||||
import { DEFAULT_SYSTEM_PROMPT } from "./const";
|
||||
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)
|
||||
@@ -12,7 +12,7 @@ export const cleanSelectedConversation = (conversation: Conversation) => {
|
||||
if (!updatedConversation.model) {
|
||||
updatedConversation = {
|
||||
...updatedConversation,
|
||||
model: updatedConversation.model || OpenAIModels[OpenAIModelID.GPT_3_5]
|
||||
model: updatedConversation.model || OpenAIModels[OpenAIModelID.GPT_3_5],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -20,14 +20,14 @@ export const cleanSelectedConversation = (conversation: Conversation) => {
|
||||
if (!updatedConversation.prompt) {
|
||||
updatedConversation = {
|
||||
...updatedConversation,
|
||||
prompt: updatedConversation.prompt || DEFAULT_SYSTEM_PROMPT
|
||||
prompt: updatedConversation.prompt || DEFAULT_SYSTEM_PROMPT,
|
||||
};
|
||||
}
|
||||
|
||||
if (!updatedConversation.folderId) {
|
||||
updatedConversation = {
|
||||
...updatedConversation,
|
||||
folderId: updatedConversation.folderId || 0
|
||||
folderId: updatedConversation.folderId || 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -56,7 +56,10 @@ export const cleanConversationHistory = (history: Conversation[]) => {
|
||||
acc.push(conversation);
|
||||
return acc;
|
||||
} catch (error) {
|
||||
console.warn(`error while cleaning conversations' history. Removing culprit`, error);
|
||||
console.warn(
|
||||
`error while cleaning conversations' history. Removing culprit`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
+25
-25
@@ -3,35 +3,35 @@ interface languageMap {
|
||||
}
|
||||
|
||||
export const programmingLanguages: languageMap = {
|
||||
javascript: ".js",
|
||||
python: ".py",
|
||||
java: ".java",
|
||||
c: ".c",
|
||||
cpp: ".cpp",
|
||||
"c++": ".cpp",
|
||||
"c#": ".cs",
|
||||
ruby: ".rb",
|
||||
php: ".php",
|
||||
swift: ".swift",
|
||||
"objective-c": ".m",
|
||||
kotlin: ".kt",
|
||||
typescript: ".ts",
|
||||
go: ".go",
|
||||
perl: ".pl",
|
||||
rust: ".rs",
|
||||
scala: ".scala",
|
||||
haskell: ".hs",
|
||||
lua: ".lua",
|
||||
shell: ".sh",
|
||||
sql: ".sql",
|
||||
html: ".html",
|
||||
css: ".css"
|
||||
javascript: '.js',
|
||||
python: '.py',
|
||||
java: '.java',
|
||||
c: '.c',
|
||||
cpp: '.cpp',
|
||||
'c++': '.cpp',
|
||||
'c#': '.cs',
|
||||
ruby: '.rb',
|
||||
php: '.php',
|
||||
swift: '.swift',
|
||||
'objective-c': '.m',
|
||||
kotlin: '.kt',
|
||||
typescript: '.ts',
|
||||
go: '.go',
|
||||
perl: '.pl',
|
||||
rust: '.rs',
|
||||
scala: '.scala',
|
||||
haskell: '.hs',
|
||||
lua: '.lua',
|
||||
shell: '.sh',
|
||||
sql: '.sql',
|
||||
html: '.html',
|
||||
css: '.css',
|
||||
// add more file extensions here, make sure the key is same as language prop in CodeBlock.tsx component
|
||||
};
|
||||
|
||||
export const generateRandomString = (length: Number, lowercase = false) => {
|
||||
const chars = "ABCDEFGHJKLMNPQRSTUVWXY3456789"; // excluding similar looking characters like Z, 2, I, 1, O, 0
|
||||
let result = "";
|
||||
const chars = 'ABCDEFGHJKLMNPQRSTUVWXY3456789'; // excluding similar looking characters like Z, 2, I, 1, O, 0
|
||||
let result = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
}
|
||||
|
||||
+4
-3
@@ -1,4 +1,5 @@
|
||||
export const DEFAULT_SYSTEM_PROMPT = "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.";
|
||||
export const DEFAULT_SYSTEM_PROMPT =
|
||||
"You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.";
|
||||
|
||||
|
||||
export const OPENAI_API_HOST = process.env.OPENAI_API_HOST || 'https://api.openai.com'
|
||||
export const OPENAI_API_HOST =
|
||||
process.env.OPENAI_API_HOST || 'https://api.openai.com';
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Conversation } from "@/types";
|
||||
import { Conversation } from '@/types';
|
||||
|
||||
export const updateConversation = (updatedConversation: Conversation, allConversations: Conversation[]) => {
|
||||
export const updateConversation = (
|
||||
updatedConversation: Conversation,
|
||||
allConversations: Conversation[],
|
||||
) => {
|
||||
const updatedConversations = allConversations.map((c) => {
|
||||
if (c.id === updatedConversation.id) {
|
||||
return updatedConversation;
|
||||
@@ -14,14 +17,14 @@ export const updateConversation = (updatedConversation: Conversation, allConvers
|
||||
|
||||
return {
|
||||
single: updatedConversation,
|
||||
all: updatedConversations
|
||||
all: updatedConversations,
|
||||
};
|
||||
};
|
||||
|
||||
export const saveConversation = (conversation: Conversation) => {
|
||||
localStorage.setItem("selectedConversation", JSON.stringify(conversation));
|
||||
localStorage.setItem('selectedConversation', JSON.stringify(conversation));
|
||||
};
|
||||
|
||||
export const saveConversations = (conversations: Conversation[]) => {
|
||||
localStorage.setItem("conversationHistory", JSON.stringify(conversations));
|
||||
localStorage.setItem('conversationHistory', JSON.stringify(conversations));
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChatFolder } from "@/types";
|
||||
import { ChatFolder } from '@/types';
|
||||
|
||||
export const saveFolders = (folders: ChatFolder[]) => {
|
||||
localStorage.setItem("folders", JSON.stringify(folders));
|
||||
localStorage.setItem('folders', JSON.stringify(folders));
|
||||
};
|
||||
|
||||
+18
-12
@@ -1,8 +1,8 @@
|
||||
import { ChatFolder, Conversation } from "@/types";
|
||||
import { ChatFolder, Conversation } from '@/types';
|
||||
|
||||
export const exportData = () => {
|
||||
let history = localStorage.getItem("conversationHistory");
|
||||
let folders = localStorage.getItem("folders");
|
||||
let history = localStorage.getItem('conversationHistory');
|
||||
let folders = localStorage.getItem('folders');
|
||||
|
||||
if (history) {
|
||||
history = JSON.parse(history);
|
||||
@@ -14,23 +14,29 @@ export const exportData = () => {
|
||||
|
||||
const data = {
|
||||
history,
|
||||
folders
|
||||
folders,
|
||||
};
|
||||
|
||||
const blob = new Blob([JSON.stringify(data)], { type: "application/json" });
|
||||
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.download = "chatbot_ui_history.json";
|
||||
const link = document.createElement('a');
|
||||
link.download = 'chatbot_ui_history.json';
|
||||
link.href = url;
|
||||
link.style.display = "none";
|
||||
link.style.display = 'none';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
export const importData = (conversations: Conversation[], folders: ChatFolder[]) => {
|
||||
localStorage.setItem("conversationHistory", JSON.stringify(conversations));
|
||||
localStorage.setItem("selectedConversation", JSON.stringify(conversations[conversations.length - 1]));
|
||||
localStorage.setItem("folders", JSON.stringify(folders));
|
||||
export const importData = (
|
||||
conversations: Conversation[],
|
||||
folders: ChatFolder[],
|
||||
) => {
|
||||
localStorage.setItem('conversationHistory', JSON.stringify(conversations));
|
||||
localStorage.setItem(
|
||||
'selectedConversation',
|
||||
JSON.stringify(conversations[conversations.length - 1]),
|
||||
);
|
||||
localStorage.setItem('folders', JSON.stringify(folders));
|
||||
};
|
||||
|
||||
+24
-15
@@ -1,27 +1,36 @@
|
||||
import { Message, OpenAIModel } from "@/types";
|
||||
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
|
||||
import { OPENAI_API_HOST } from "../app/const";
|
||||
import { Message, OpenAIModel } from '@/types';
|
||||
import {
|
||||
createParser,
|
||||
ParsedEvent,
|
||||
ReconnectInterval,
|
||||
} from 'eventsource-parser';
|
||||
import { OPENAI_API_HOST } from '../app/const';
|
||||
|
||||
export const OpenAIStream = async (model: OpenAIModel, systemPrompt: string, key: string, messages: Message[]) => {
|
||||
export const OpenAIStream = async (
|
||||
model: OpenAIModel,
|
||||
systemPrompt: string,
|
||||
key: string,
|
||||
messages: Message[],
|
||||
) => {
|
||||
const res = await fetch(`${OPENAI_API_HOST}/v1/chat/completions`, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`,
|
||||
},
|
||||
method: "POST",
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
model: model.id,
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: systemPrompt
|
||||
role: 'system',
|
||||
content: systemPrompt,
|
||||
},
|
||||
...messages
|
||||
...messages,
|
||||
],
|
||||
max_tokens: 1000,
|
||||
temperature: 1,
|
||||
stream: true
|
||||
})
|
||||
stream: true,
|
||||
}),
|
||||
});
|
||||
|
||||
if (res.status !== 200) {
|
||||
@@ -35,10 +44,10 @@ export const OpenAIStream = async (model: OpenAIModel, systemPrompt: string, key
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
const onParse = (event: ParsedEvent | ReconnectInterval) => {
|
||||
if (event.type === "event") {
|
||||
if (event.type === 'event') {
|
||||
const data = event.data;
|
||||
|
||||
if (data === "[DONE]") {
|
||||
if (data === '[DONE]') {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
@@ -59,7 +68,7 @@ export const OpenAIStream = async (model: OpenAIModel, systemPrompt: string, key
|
||||
for await (const chunk of res.body as any) {
|
||||
parser.feed(decoder.decode(chunk));
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return stream;
|
||||
|
||||
Reference in New Issue
Block a user