Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Set the language used for the chat
# Default to English(EN), optional in Chinese(CH)
CHAT_LANGUAGE=EN

OPENAI_API_KEY=

# Update these with your pinecone details from your dashboard.
Expand Down
47 changes: 36 additions & 11 deletions utils/makechain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@ import { OpenAI } from 'langchain/llms/openai';
import { PineconeStore } from 'langchain/vectorstores/pinecone';
import { ConversationalRetrievalQAChain } from 'langchain/chains';

const CONDENSE_PROMPT = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
const PROMPT: { [key: string]: { CONDENSE_PROMPT: string; QA_PROMPT: string; } } = {
'CH':{
CONDENSE_PROMPT: `给定以下对话和后续问题,将后续问题重新表述为独立问题。

Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`;
对话历史:
{chat_history}
后续输入: {question}
独立问题:`,
QA_PROMPT:`你是一个有用的人工智能助手。使用以下上下文来回答最后的问题。

const QA_PROMPT = `You are a helpful AI assistant. Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.
如果你不知道答案,就说你不知道。不要试图编造一个答案。

如果问题与上下文无关,请礼貌地回答,您将只回答与上下文相关的问题。

{context}

问题: {question}
markdown格式的有用答案:`
},
'EN':{
CONDENSE_PROMPT: `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.

{context}
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`,
QA_PROMPT:`You are a helpful AI assistant. Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.

{context}

Question: {question}
Helpful answer in markdown:`
}
}

Question: {question}
Helpful answer in markdown:`;
const LANGUAGE:string = process.env['CHAT_LANGUAGE'] || 'EN';

const { CONDENSE_PROMPT, QA_PROMPT } = PROMPT[LANGUAGE];

export const makeChain = (vectorstore: PineconeStore) => {
const model = new OpenAI({
Expand Down