From 0da2cacacfdddf12f5c322ae240352c25735d947 Mon Sep 17 00:00:00 2001 From: pranav-kural Date: Tue, 9 Jul 2024 02:18:35 -0400 Subject: [PATCH] updated documentation to reflect recent changes in qvikchat package --- pages/_meta.json | 4 +- .../chat-endpoint-configurations.mdx | 145 ++++++++++++------ pages/chat-endpoints/rag-chat.mdx | 3 + pages/getting-started.mdx | 16 ++ 4 files changed, 118 insertions(+), 50 deletions(-) diff --git a/pages/_meta.json b/pages/_meta.json index de87143..1729ef0 100644 --- a/pages/_meta.json +++ b/pages/_meta.json @@ -2,7 +2,9 @@ "index": "Introduction", "getting-started": "Getting Started", "chat-endpoints": "Chat Endpoints", - "chat-agent": "Chat Agent", + "chat-agent": { + "display": "hidden" + }, "chat-history": "Chat History", "prompts": "Prompts", "rag-guide": "RAG Guide", diff --git a/pages/chat-endpoints/chat-endpoint-configurations.mdx b/pages/chat-endpoints/chat-endpoint-configurations.mdx index 4e1c751..70d8cca 100644 --- a/pages/chat-endpoints/chat-endpoint-configurations.mdx +++ b/pages/chat-endpoints/chat-endpoint-configurations.mdx @@ -8,54 +8,101 @@ import { defineChatEndpoint } from "@oconva/qvikchat/endpoints"; Below are some of the chat endpoint configurations you can define. +- `chatAgentConfig`: Configurations for the chat agent, like LLM model, system prompt, chat prompt, and tools. +- `endpoint`: Server endpoint to which queries should be sent to run this chat flow. + +**Close-ended Chat Configurations** + +- `agentType`: Type of chat agent to use for this endpoint. Can set it to "close-ended" to create a close-ended chat endpoint. By default, it is set to "open-ended". +- `topic`: Topic for close-ended chat agent. Required if RAG is enabled. Queries are restricted to be relevant to the given topic so to prevent unintended use. + +**Chat History Configurations** + +- `enableChatHistory`: Enable chat history for this endpoint. If chat ID is provided, chat history will be fetched and used to generate response. If no chat ID is provided, a new chat ID will be generated to store chat history, and will be returned in the response. +- `chatHistoryStore`: Chat History Store instance to use for this endpoint. + +**Auth Configurations** + +- `enableAuth`: Enable authentication for this endpoint. Must provide an API Key Store instance if set to true. +- `apiKeyStore`: API Key Store instance to use for this endpoint. + +**Cache Configurations** + +- `enableCache`: Enable caching for this endpoint. Must provide a Cache Store instance if set to true. +- `cacheStore`: Cache Store instance to use for this endpoint. + +**RAG Configurations** + +- `topic`: Topic for RAG chat agent. Required if RAG is enabled. Queries are restricted to be relevant to the given topic so to prevent unintended use. +- `enableRAG`: Enable RAG (Retrieval Augmented Generation) functionality for this endpoint. Must provide a retriever method if set to true. +- `retriever`: Method to retrieve documents for RAG. Can be obtained from the `getDataRetriever` method. +- `retrieverConfig`: Configuration for the RAG retriever, for example, number of documents to retrieve, algorithm to use, etc. + +## Example + +Below is an example of a comprehensively configured chat endpoint. You don't need to provide all the configurations, only the ones you need. Below example is only for demonstration purposes. + ```typescript copy -/** - * Represents the parameters for defining a chat endpoint. - */ -export interface DefineChatEndpointParams { - /** - * The chat agent used for the endpoint. - */ - chatAgent: ChatAgent; - - /** - * The endpoint for the chat endpoint. - */ - endpoint: string; - - /** - * Optional flag to enable chat history. - */ - enableChatHistory?: boolean; - - /** - * Optional flag to enable authentication. - */ - enableAuth?: boolean; - - /** - * Optional API key store. - */ - apiKeyStore?: APIKeyStore; - - /** - * Optional flag to enable caching. - */ - enableCache?: boolean; - - /** - * Optional cache store. - */ - cacheStore?: CacheStore; - - /** - * Optional flag to enable RAG (Retrieve, Answer, Generate) functionality. - */ - enableRAG?: boolean; - - /** - * Optional method to retrieve documents for RAG. - */ - retriever?: TextDataRetriever; -} +import { FirestoreAPIKeyStore } from "@oconva/qvikchat/auth"; +import { FirestoreCacheStore } from "@oconva/qvikchat/cache"; +import { getEmbeddingModel } from "@oconva/qvikchat/data-embeddings"; +import { TaskType } from "@oconva/qvikchat/data-retrievers"; +import { defineChatEndpoint } from "@oconva/qvikchat/endpoints"; +import { FirestoreChatHistoryStore } from "@oconva/qvikchat/history"; +import { credential, getFirebaseApp } from "@oconva/qvikchat/firebase"; + +// Initialize Firebase app +const firebaseApp = getFirebaseApp({ + credential: credential.cert( + process.env.GOOGLE_APPLICATION_CREDENTIALS as string + ), +}); + +// Define chat endpoint with RAG, chat history, cache and auth +// uses Firestore API key store, Firestore chat history store, Firestore cache store +// uses Gemini 15 Pro model for chat and embedding-001 embedding model +// uses custom retrieval strategy for RAG +defineChatEndpoint({ + endpoint: "chat", + topic: "inventory data", + chatAgentConfig: { + model: "gemini15Pro", + modelConfig: { + temperature: 0.9, + }, + }, + enableAuth: true, + apiKeyStore: new FirestoreAPIKeyStore({ + firebaseApp: firebaseApp, + collectionName: "api-keys", + }), + enableChatHistory: true, + chatHistoryStore: new FirestoreChatHistoryStore({ + firebaseApp: firebaseApp, + collectionName: "chat-history", + }), + enableCache: true, + cacheStore: new FirestoreCacheStore({ + firebaseApp: firebaseApp, + collectionName: "cache", + }), + enableRAG: true, + retrieverConfig: { + dataType: "csv", + filePath: "data/inventory.csv", + csvLoaderOptions: { + column: "products", + separator: ",", + }, + generateEmbeddings: true, + retrievalOptions: { + k: 10, + searchType: "mmr", + }, + embeddingModel: getEmbeddingModel({ + modelName: "embedding-001", + taskType: TaskType.RETRIEVAL_DOCUMENT, + }), + }, +}); ``` diff --git a/pages/chat-endpoints/rag-chat.mdx b/pages/chat-endpoints/rag-chat.mdx index 236d3a0..9c2f318 100644 --- a/pages/chat-endpoints/rag-chat.mdx +++ b/pages/chat-endpoints/rag-chat.mdx @@ -77,6 +77,7 @@ const inventoryDataRetriever = await getDataRetriever({ // Inventory Data chat endpoint with support for RAG defineChatEndpoint({ endpoint: "chat-rag-inventory", + topic: "inventory data", enableRAG: true, retriever: inventoryDataRetriever, }); @@ -164,6 +165,7 @@ const inventoryDataRetriever = await getDataRetriever({ // Inventory Data chat endpoint with support for RAG and chat history defineChatEndpoint({ endpoint: "chat-rag-history-inventory", + topic: "inventory data", enableRAG: true, retriever: inventoryDataRetriever, enableChatHistory: true, @@ -263,6 +265,7 @@ const inventoryDataRetriever = await getDataRetriever({ // Inventory Data chat endpoint with support for RAG, chat history, cache and auth defineChatEndpoint({ endpoint: "chat-rag-history-auth-cached-inventory", + topic: "inventory data", enableChatHistory: true, chatHistoryStore: new InMemoryChatHistoryStore(), enableAuth: true, diff --git a/pages/getting-started.mdx b/pages/getting-started.mdx index a622267..eecfbca 100644 --- a/pages/getting-started.mdx +++ b/pages/getting-started.mdx @@ -18,6 +18,22 @@ Or pnpm add @oconva/qvikchat ``` +## Setup Environment Variables + +Create a `.env` file in the root of the project and add the following environment variables: + +```env copy +GOOGLE_GENAI_API_KEY= +OPENAI_API_KEY= +GOOGLE_APPLICATION_CREDENTIALS= +``` + +Alternatively, you can copy the `.env.tmp` file or rename it to `.env` and fill in the values. + +By default QvikChat uses the Google GenAI, so to use QvikChat with default settings, you need to provide the `GOOGLE_GENAI_API_KEY`. You don't have to set values for other environment variables if you are using the default settings. + +Add value to the `OPENAI_API_KEY` variable if you're going to use OpenAI models and to the `GOOGLE_APPLICATION_CREDENTIALS` variable if you're going to use Firebase Firestore. + ## Usage Before you can deploy your chat endpoints, you need to setup Firebase Genkit, either by using the default configurations or by providing your own configurations, these may include additional Genkit plugins you may want to enable (e.g. to add support for a new language model). When starting out, we recommend using the default configurations.