Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
thivy authored Aug 4, 2023
2 parents eaad5d2 + 063cbe0 commit f87afc8
Show file tree
Hide file tree
Showing 29 changed files with 254 additions and 223 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ Clone this repository locally or fork to your Github account. Run all of the the
NEXTAUTH_SECRET=
NEXTAUTH_URL=http://localhost:3000
AZURE_COSMOSEDB_URI=
AZURE_COSMOSEDB_KEY=
AZURE_COSMOSDB_URI=
AZURE_COSMOSDB_KEY=
```

4. Install npm packages by running `npm install`
Expand Down Expand Up @@ -233,7 +233,7 @@ Below are the required environment variables, to be added to the Azure Portal or
| AUTH_GITHUB_ID | | Client ID of your GitHub OAuth application |
| AUTH_GITHUB_SECRET | | Client Secret of your GitHub OAuth application |
| NEXTAUTH_SECRET | | Used to encrypt the NextAuth.js JWT, and to hash email verification tokens. **This set by default as part of the deployment template** |
| NEXTAUTH_URL | | Current webs hosting domain name with HTTP or HTTPS. **This set by default as part of the deployment template** |
| NEXTAUTH_URL | | Current webs hosting domain name with HTTP or HTTPS. **This set by default as part of the deployment template** |
| AZURE_COSMOSEDB_URI | | URL of the Azure CosmosDB |
| AZURE_COSMOSEDB_KEY | | API Key for Azure Cosmos DB |
| AZURE_AD_CLIENT_ID | | The client id specific to the application |
Expand Down
6 changes: 3 additions & 3 deletions infra/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@
"appCommandLine": "node server.js",
"appSettings": [
{
"name": "AZURE_COSMOSEDB_URI",
"name": "AZURE_COSMOSDB_URI",
"value": "[reference(resourceId('Microsoft.DocumentDB/databaseAccounts', format('{0}-cosmos-{1}', parameters('name'), parameters('resourceToken'))), '2023-04-15').documentEndpoint]"
},
{
"name": "AZURE_COSMOSEDB_KEY",
"name": "AZURE_COSMOSDB_KEY",
"value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', format('{0}-cosmos-{1}', parameters('name'), parameters('resourceToken'))), '2023-04-15').primaryMasterKey]"
},
{
Expand Down Expand Up @@ -207,4 +207,4 @@
}
}
]
}
}
4 changes: 2 additions & 2 deletions infra/resources.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ resource webApp 'Microsoft.Web/sites@2020-06-01' = {
appCommandLine: 'node server.js'
appSettings: [
{
name: 'AZURE_COSMOSEDB_URI'
name: 'AZURE_COSMOSDB_URI'
value: cosmosDbAccount.properties.documentEndpoint
}
{
name: 'AZURE_COSMOSEDB_KEY'
name: 'AZURE_COSMOSDB_KEY'
value: cosmosDbAccount.listKeys().primaryMasterKey
}
{
Expand Down
2 changes: 1 addition & 1 deletion src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
AZURE_OPENAI_API_KEY=
AZURE_OPENAI_API_INSTANCE_NAME=
AZURE_OPENAI_API_DEPLOYMENT_NAME=
AZURE_OPENAI_API_VERSION=2023-05-15
AZURE_OPENAI_API_VERSION=2023-03-15-preview

# Use GitHub environment variables if you’re not using Azure AD auth
AUTH_GITHUB_ID=
Expand Down
6 changes: 3 additions & 3 deletions src/app/chat/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FindAllChats } from "@/features/chat/chat-service";
import { FindChatThreadByID } from "@/features/chat/chat-thread-service";
import { ChatUI } from "@/features/chat/chat-ui";
import { FindAllChats } from "@/features/chat/chat-services/chat-service";
import { FindChatThreadByID } from "@/features/chat/chat-services/chat-thread-service";
import { ChatUI } from "@/features/chat/chat-ui/chat-ui";
import { notFound } from "next/navigation";

export default async function Home({ params }: { params: { id: string } }) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/chat/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Card } from "@/components/ui/card";
import { NewChat } from "@/features/chat/chat-menu/new-chat";
import { FindAllChatThreadForCurrentUser } from "@/features/chat/chat-thread-service";
import { FindAllChatThreadForCurrentUser } from "@/features/chat/chat-services/chat-thread-service";
import { redirect } from "next/navigation";

export default async function Home() {
Expand Down
4 changes: 2 additions & 2 deletions src/components/chat/chat-row.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chatRole } from "@/features/chat/chat-service";
import { ChatRole } from "@/features/chat/chat-services/models";
import { cn } from "@/lib/utils";
import { FC } from "react";
import remarkGfm from "remark-gfm";
Expand All @@ -11,7 +11,7 @@ interface ChatRowProps {
name: string;
profilePicture: string;
message: string;
type: chatRole;
type: ChatRole;
}

const ChatRow: FC<ChatRowProps> = (props) => {
Expand Down
4 changes: 2 additions & 2 deletions src/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ ENV HOSTNAME localhost
# ENV NEXTAUTH_SECRET
# ENV NEXTAUTH_URL

# ENV AZURE_COSMOSEDB_URI
# ENV AZURE_COSMOSEDB_KEY
# ENV AZURE_COSMOSDB_URI
# ENV AZURE_COSMOSDB_KEY

EXPOSE 3000

Expand Down
38 changes: 0 additions & 38 deletions src/features/chat/chat-api-helpers.ts

This file was deleted.

67 changes: 8 additions & 59 deletions src/features/chat/chat-api.ts
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);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
import {
AzureCogDocument,
AzureCogSearch,
} from "../azure-cog-search/azure-cog-vector-store";
import { PromptGPTProps, initAndGuardChatSession } from "./chat-api-helpers";
import { ChatMessageModel, inertPromptAndResponse } from "./chat-service";
} from "../../langchain/vector-stores/azure-cog-search/azure-cog-vector-store";
import { insertPromptAndResponse } from "../chat-services/chat-service";
import { initAndGuardChatSession } from "../chat-services/chat-thread-service";
import { ChatMessageModel, PromptGPTProps } from "../chat-services/models";

export interface FaqDocumentIndex extends AzureCogDocument {
id: string;
Expand All @@ -23,7 +24,7 @@ export interface FaqDocumentIndex extends AzureCogDocument {
metadata: any;
}

export const PromptDataGPT = async (props: PromptGPTProps) => {
export const ChatData = async (props: PromptGPTProps) => {
const { lastHumanMessage, id, chats } = await initAndGuardChatSession(props);

const chatModel = new ChatOpenAI({
Expand All @@ -40,7 +41,7 @@ export const PromptDataGPT = async (props: PromptGPTProps) => {
});
const { stream, handlers } = LangChainStream({
onCompletion: async (completion: string) => {
await inertPromptAndResponse(id, lastHumanMessage.content, completion);
await insertPromptAndResponse(id, lastHumanMessage.content, completion);
},
});

Expand Down
32 changes: 0 additions & 32 deletions src/features/chat/chat-helpers.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/features/chat/chat-menu/chat-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Menu, MenuContent, MenuFooter, MenuHeader } from "@/components/menu";
import { FindAllChatThreadForCurrentUser } from "@/features/chat/chat-thread-service";
import { FindAllChatThreadForCurrentUser } from "@/features/chat/chat-services/chat-thread-service";
import { ThemeToggle } from "@/features/theme/theme-toggle";
import { MenuItems } from "./menu-items";
import { NewChat } from "./new-chat";
Expand Down
6 changes: 2 additions & 4 deletions src/features/chat/chat-menu/menu-items.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"use client";
import { MenuItem } from "@/components/menu";
import { Button } from "@/components/ui/button";
import {
ChatThreadModel,
SoftDeleteChatThreadByID,
} from "@/features/chat/chat-thread-service";
import { SoftDeleteChatThreadByID } from "@/features/chat/chat-services/chat-thread-service";
import { MessageCircle, Trash } from "lucide-react";
import { useParams, useRouter } from "next/navigation";
import { FC } from "react";
import { ChatThreadModel } from "../chat-services/models";

interface Prop {
menuItems: Array<ChatThreadModel>;
Expand Down
2 changes: 1 addition & 1 deletion src/features/chat/chat-menu/new-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Button } from "@/components/ui/button";
import { PlusCircle } from "lucide-react";
import { useRouter } from "next/navigation";
import { CreateChatThread } from "../chat-thread-service";
import { CreateChatThread } from "../chat-services/chat-thread-service";

export const NewChat = () => {
const router = useRouter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import "server-only";

import { SqlQuerySpec } from "@azure/cosmos";
import { nanoid } from "nanoid";
import { memoryContainer } from "../common/cosmos";
import { memoryContainer } from "../../common/cosmos";
import { ChatMessageModel, MESSAGE_ATTRIBUTE } from "./models";

export const FindAllChats = async (chatThreadID: string) => {
const container = await memoryContainer();
Expand Down Expand Up @@ -47,7 +48,7 @@ export const UpsertChat = async (chatModel: ChatMessageModel) => {
await container.items.upsert(modelToSave);
};

export const inertPromptAndResponse = async (
export const insertPromptAndResponse = async (
threadID: string,
userQuestion: string,
assistantResponse: string
Expand Down Expand Up @@ -78,18 +79,3 @@ export const newChatModel = (): ChatMessageModel => {
isDeleted: false,
};
};

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";

const MESSAGE_ATTRIBUTE = "CHAT_MESSAGE";
Loading

0 comments on commit f87afc8

Please sign in to comment.