-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
254 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,11 @@ | ||
import { LangChainStream, StreamingTextResponse } from "ai"; | ||
import { ConversationChain } from "langchain/chains"; | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { BufferWindowMemory } from "langchain/memory"; | ||
import { | ||
ChatPromptTemplate, | ||
HumanMessagePromptTemplate, | ||
MessagesPlaceholder, | ||
SystemMessagePromptTemplate, | ||
} from "langchain/prompts"; | ||
import { userHashedId } from "../auth/helpers"; | ||
import { CosmosDBChatMessageHistory } from "../langchain/stores/cosmosdb"; | ||
import { PromptGPTProps, initAndGuardChatSession } from "./chat-api-helpers"; | ||
import { ChatData } from "./chat-data/chat-data-api"; | ||
import { PromptGPTProps } from "./chat-services/models"; | ||
import { ChatSimple } from "./chat-simple/chat-simple-api"; | ||
|
||
export const PromptGPT = async (props: PromptGPTProps) => { | ||
const { lastHumanMessage, id } = await initAndGuardChatSession(props); | ||
|
||
const { stream, handlers } = LangChainStream(); | ||
|
||
const userId = await userHashedId(); | ||
|
||
const chat = new ChatOpenAI({ | ||
temperature: 0, | ||
streaming: true, | ||
}); | ||
|
||
const memory = new BufferWindowMemory({ | ||
k: 100, | ||
returnMessages: true, | ||
memoryKey: "history", | ||
chatHistory: new CosmosDBChatMessageHistory({ | ||
sessionId: id, | ||
userId: userId, | ||
config: { | ||
db: process.env.AZURE_COSMOSEDBDB_DB_NAME, | ||
container: process.env.AZURE_COSMOSEDBDB_CONTAINER_NAME, | ||
endpoint: process.env.AZURE_COSMOSEDB_URI, | ||
key: process.env.AZURE_COSMOSEDB_KEY, | ||
partitionKey: "id", | ||
}, | ||
}), | ||
}); | ||
|
||
const chatPrompt = ChatPromptTemplate.fromPromptMessages([ | ||
SystemMessagePromptTemplate.fromTemplate( | ||
`-You are Azure ChatGPT who is a helpful AI Assistant. | ||
- You will provide clear and concise queries, and you will respond with polite and professional answers. | ||
- You will answer questions truthfully and accurately.` | ||
), | ||
new MessagesPlaceholder("history"), | ||
HumanMessagePromptTemplate.fromTemplate("{input}"), | ||
]); | ||
const chain = new ConversationChain({ | ||
llm: chat, | ||
memory, | ||
prompt: chatPrompt, | ||
}); | ||
|
||
chain.call({ input: lastHumanMessage.content }, [handlers]); | ||
|
||
return new StreamingTextResponse(stream); | ||
if (props.chatType === "simple") { | ||
return await ChatSimple(props); | ||
} else if (props.chatType === "data") { | ||
return await ChatData(props); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.