Skip to content

Commit

Permalink
- folder restructure
Browse files Browse the repository at this point in the history
- fix spell mistake in env variables
  • Loading branch information
thivy committed Aug 3, 2023
1 parent d53039e commit 861080e
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 40 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,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=
```

2. Install npm packages by running `npm install`
Expand Down Expand Up @@ -228,8 +228,8 @@ Below are the required environment variables
| 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** qweqweqwe |
| AZURE_COSMOSEDB_URI | | URL of the Azure CosmosDB |
| AZURE_COSMOSEDB_KEY | | API Key for Azure Cosmos DB |
| AZURE_COSMOSDB_URI | | URL of the Azure CosmosDB |
| AZURE_COSMOSDB_KEY | | API Key for Azure Cosmos DB |
| AZURE_AD_CLIENT_ID | | The client id specific to the application |
| AZURE_AD_CLIENT_SECRET | | The client secret specific to the application |
| AZURE_AD_TENANT_ID | | The organisation Tenant ID |
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
4 changes: 2 additions & 2 deletions src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ AZURE_AD_TENANT_ID=
NEXTAUTH_SECRET=this-is-a-secret-with-minimum-length-of-32-characters
NEXTAUTH_URL=http://localhost:3000 # add your production URL here (and remove this comment)

AZURE_COSMOSEDB_URI=
AZURE_COSMOSEDB_KEY=
AZURE_COSMOSDB_URI=
AZURE_COSMOSDB_KEY=
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
4 changes: 2 additions & 2 deletions src/features/chat/chat-api-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
import {
AzureCogDocument,
AzureCogSearch,
} from "../azure-cog-search/azure-cog-vector-store";
import { PromptGPTProps, initAndGuardChatSession } from "./chat-api-helpers";
} from "../langchain/vector-stores/azure-cog-search/azure-cog-vector-store";
import { PromptGPTProps, initAndGuardChatSession } from "./chat-api-utils";
import { ChatMessageModel, inertPromptAndResponse } from "./chat-service";

export interface FaqDocumentIndex extends AzureCogDocument {
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions src/features/chat/chat-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
SystemMessagePromptTemplate,
} from "langchain/prompts";
import { userHashedId } from "../auth/helpers";
import { CosmosDBChatMessageHistory } from "../langchain/stores/cosmosdb";
import { PromptGPTProps, initAndGuardChatSession } from "./chat-api-helpers";
import { CosmosDBChatMessageHistory } from "../langchain/memory/cosmosdb/cosmosdb";
import { PromptGPTProps, initAndGuardChatSession } from "./chat-api-utils";

export const PromptGPT = async (props: PromptGPTProps) => {
const { lastHumanMessage, id } = await initAndGuardChatSession(props);
Expand All @@ -32,10 +32,10 @@ export const PromptGPT = async (props: PromptGPTProps) => {
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,
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",
},
}),
Expand Down
3 changes: 1 addition & 2 deletions src/features/chat/chat-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "server-only";
import { SqlQuerySpec } from "@azure/cosmos";
import { nanoid } from "nanoid";
import { memoryContainer } from "../common/cosmos";
import { MESSAGE_ATTRIBUTE } from "./models";

export const FindAllChats = async (chatThreadID: string) => {
const container = await memoryContainer();
Expand Down Expand Up @@ -91,5 +92,3 @@ export interface ChatMessageModel {
}

export type chatRole = "system" | "user" | "assistant" | "function";

const MESSAGE_ATTRIBUTE = "CHAT_MESSAGE";
7 changes: 4 additions & 3 deletions src/features/chat/chat-thread-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ChatMessageModel, FindAllChats } from "@/features/chat/chat-service";
import { SqlQuerySpec } from "@azure/cosmos";
import { nanoid } from "nanoid";
import { memoryContainer } from "../common/cosmos";
import { CHAT_THREAD_ATTRIBUTE } from "./models";

export const FindAllChatThreadForCurrentUser = async () => {
const container = await memoryContainer();
Expand All @@ -16,7 +17,7 @@ export const FindAllChatThreadForCurrentUser = async () => {
parameters: [
{
name: "@type",
value: "CHAT_THREAD",
value: CHAT_THREAD_ATTRIBUTE,
},
{
name: "@userId",
Expand Down Expand Up @@ -44,7 +45,7 @@ export const FindChatThreadByID = async (id: string) => {
parameters: [
{
name: "@type",
value: "CHAT_THREAD",
value: CHAT_THREAD_ATTRIBUTE,
},
{
name: "@userId",
Expand Down Expand Up @@ -134,7 +135,7 @@ export const CreateChatThread = async () => {
id: nanoid(),
createdAt: new Date(),
isDeleted: false,
type: "CHAT_THREAD",
type: CHAT_THREAD_ATTRIBUTE,
};

const container = await memoryContainer();
Expand Down
4 changes: 2 additions & 2 deletions src/features/chat/chat-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { useChat } from "ai/react";
import { useSession } from "next-auth/react";
import { useParams } from "next/navigation";
import { FC, FormEvent, useRef, useState } from "react";
import { PromptGPTBody } from "./chat-api-helpers";
import { transformCosmosToAIModel } from "./chat-helpers";
import { PromptGPTBody } from "./chat-api-utils";
import { ChatMessageModel } from "./chat-service";
import { transformCosmosToAIModel } from "./utils";

interface Prop {
chats: Array<ChatMessageModel>;
Expand Down
2 changes: 2 additions & 0 deletions src/features/chat/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CHAT_THREAD_ATTRIBUTE = "CHAT_THREAD";
export const MESSAGE_ATTRIBUTE = "CHAT_MESSAGE";
File renamed without changes.
4 changes: 2 additions & 2 deletions src/features/common/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const DB_NAME = "chat";
const CONTAINER_NAME = "history";

export const memoryContainer = async () => {
const endpoint = process.env.AZURE_COSMOSEDB_URI!;
const key = process.env.AZURE_COSMOSEDB_KEY!;
const endpoint = process.env.AZURE_COSMOSDB_URI!;
const key = process.env.AZURE_COSMOSDB_KEY!;
const client = new CosmosClient({ endpoint, key });

await client.databases.createIfNotExists({ id: DB_NAME });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
FindAllChats,
UpsertChat,
} from "@/features/chat/chat-service";
import { MESSAGE_ATTRIBUTE } from "@/features/chat/models";
import { CosmosClient } from "@azure/cosmos";
import { StoredMessage } from "langchain/schema";

Expand Down Expand Up @@ -37,7 +38,7 @@ export const getChatMessages = async (
const ms: StoredMessage[] = [];
items.forEach((item) => {
ms.push({
type: "CHAT_MESSAGE",
type: MESSAGE_ATTRIBUTE,
data: {
content: item.content,
role: item.role === "user" ? "human" : "ai",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { ChatMessageModel } from "@/features/chat/chat-service";
import { MESSAGE_ATTRIBUTE } from "@/features/chat/models";
import { CosmosClient } from "@azure/cosmos";
import {
AIMessage,
BaseListChatMessageHistory,
BaseMessage,
} from "langchain/schema";
import { nanoid } from "nanoid";
import { mapStoredMessagesToChatMessages } from "../utils";
import {
addChatMessage,
getChatMessages,
initChatContainer,
} from "./cosmosdb-chat-service";
import { mapStoredMessagesToChatMessages } from "./utils";

export interface CosmosDBClientConfig {
db: string;
Expand Down Expand Up @@ -60,7 +61,7 @@ export class CosmosDBChatMessageHistory extends BaseListChatMessageHistory {
const modelToSave: ChatMessageModel = {
id: nanoid(),
createdAt: new Date(),
type: "CHAT_MESSAGE",
type: MESSAGE_ATTRIBUTE,
isDeleted: false,
content: message.content,
role: message instanceof AIMessage ? "assistant" : "user",
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions src/features/reporting/reporting-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SqlQuerySpec } from "@azure/cosmos";
import { ChatMessageModel } from "../chat/chat-service";
import { ChatThreadModel } from "../chat/chat-thread-service";
import { CHAT_THREAD_ATTRIBUTE, MESSAGE_ATTRIBUTE } from "../chat/models";
import { memoryContainer } from "../common/cosmos";

export const FindAllChatThreadsForReporting = async (
Expand All @@ -16,7 +17,7 @@ export const FindAllChatThreadsForReporting = async (
parameters: [
{
name: "@type",
value: "CHAT_THREAD",
value: CHAT_THREAD_ATTRIBUTE,
},
],
};
Expand All @@ -37,7 +38,7 @@ export const FindChatThreadByID = async (chatThreadID: string) => {
parameters: [
{
name: "@type",
value: "CHAT_THREAD",
value: CHAT_THREAD_ATTRIBUTE,
},

{
Expand All @@ -62,7 +63,7 @@ export const FindAllChatsInThread = async (chatThreadID: string) => {
parameters: [
{
name: "@type",
value: "CHAT_MESSAGE",
value: MESSAGE_ATTRIBUTE,
},
{
name: "@threadId",
Expand Down
8 changes: 4 additions & 4 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const azureEnvVars = [
"AZURE_OPENAI_API_DEPLOYMENT_NAME",
"AZURE_OPENAI_API_VERSION",
"AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME",
"AZURE_COSMOSEDB_URI",
"AZURE_COSMOSEDB_KEY",
"AZURE_COSMOSEDBDB_DB_NAME",
"AZURE_COSMOSEDBDB_CONTAINER_NAME",
"AZURE_COSMOSDB_URI",
"AZURE_COSMOSDB_KEY",
"AZURE_COSMOSDB_DB_NAME",
"AZURE_COSMOSDB_CONTAINER_NAME",
"AZURE_SEARCH_API_KEY",
"AZURE_SEARCH_NAME",
"AZURE_SEARCH_INDEX_NAME",
Expand Down

0 comments on commit 861080e

Please sign in to comment.