forked from matijagrcic/azurechatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code clean up so that it's simpler to extend the chat with advance sc…
…enarios - refactor chat folder - move services and models into one folder
- Loading branch information
Showing
21 changed files
with
228 additions
and
202 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 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/memory/cosmosdb/cosmosdb"; | ||
import { PromptGPTProps, initAndGuardChatSession } from "./chat-api-utils"; | ||
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_COSMOSDB_DB_NAME, | ||
container: process.env.AZURE_COSMOSDB_CONTAINER_NAME, | ||
endpoint: process.env.AZURE_COSMOSDB_URI, | ||
key: process.env.AZURE_COSMOSDB_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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Message } from "ai"; | ||
|
||
export const CHAT_THREAD_ATTRIBUTE = "CHAT_THREAD"; | ||
export const MESSAGE_ATTRIBUTE = "CHAT_MESSAGE"; | ||
|
||
export interface ChatMessageModel { | ||
id: string; | ||
createdAt: Date; | ||
isDeleted: boolean; | ||
threadId: string; | ||
userId: string; | ||
content: string; | ||
role: ChatRole; | ||
type: "CHAT_MESSAGE"; | ||
} | ||
|
||
export type ChatRole = "system" | "user" | "assistant" | "function"; | ||
|
||
export type ChatType = "simple" | "data" | "mssql"; | ||
|
||
export interface ChatThreadModel { | ||
id: string; | ||
name: string; | ||
model: string; | ||
createdAt: Date; | ||
userId: string; | ||
useName: string; | ||
isDeleted: boolean; | ||
chatType: ChatType; | ||
type: "CHAT_THREAD"; | ||
} | ||
|
||
export interface PromptGPTBody { | ||
id: string; // thread id | ||
model: string; // model name | ||
chatType: ChatType; | ||
} | ||
|
||
export interface PromptGPTProps extends PromptGPTBody { | ||
messages: Message[]; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Message } from "ai"; | ||
import { ChatMessageModel } from "./models"; | ||
|
||
export const transformCosmosToAIModel = ( | ||
chats: Array<ChatMessageModel> | ||
): Array<Message> => { | ||
return chats.map((chat) => { | ||
return { | ||
role: chat.role, | ||
content: chat.content, | ||
id: chat.id, | ||
createdAt: chat.createdAt, | ||
}; | ||
}); | ||
}; |
Oops, something went wrong.