Close-ended Chat
Chat endpoint where user queries are restricted to a specific topic. Prevents misuse of the chat service by restricting the usage, for example, a chat service meant to answer queries related to a specific topic like Firebase won't answer user queries related to other topics like solving a calculus assignment question.
You can define an close-ended chat endpoint with various features such as chat history, response caching, and API key authentication, by simply modifying the configurations provided to the defineChatEndpoint
function. To use this, first import the method as following:
import { defineChatEndpoint } from "@oconva/qvikchat/endpoints";
Below are some of the close-ended chat endpoints you can define.
Close Chat
Restricted chat to a specific topic with no chat history support.
Usage
Below is an example of how you can define a close-ended chat endpoint. You can use the topic
field to specify the topic for which this endpoint should answer queries.
// Close-ended chat endpoint
// will only answer queries related to specified topic, in this case, 'Firebase'
defineChatEndpoint({
endpoint: "chat-close",
agentType: "close-ended",
topic: "Firebase",
});
Input Schema
Expects an object with the following properties:
-
query
: User query to chat agent.{ "query": "string" }
Output Schema
For successful requests, the response will contain:
-
response
: Chat agent response to the user query, returned as a string by default. (You may add more endpoints or update existing ones undersrc/endpoints
){ "response": "string" }
For failed requests, the response will contain:
-
error
: Error message returned by the server.{ "error": "string" }
Testing
Can use commands below to test the endpoint defined in the example above.
For the first example, ideally, you should get a response that the model can't answer the question since it's not related to the specified topic, establishing that the chat agent is working as expected. It should, however, generate a proper answer for the second example.
curl -X POST "http://127.0.0.1:3400/chat-close" -H "Content-Type: application/json" -d '{"data": { "query" : "Can you help me with my calculus assignment?" } }'
curl -X POST "http://127.0.0.1:3400/chat-close" -H "Content-Type: application/json" -d '{"data": { "query" : "What is App check?" } }'
Genkit Developer UI
You could also start the Genkit Developer UI to test above:
npx genkit start
Endpoint to test: chat-close
Close Chat with History
Restricted chat to a specific topic with chat history support.
Usage
Below is an example of how you can define a close-ended chat endpoint that supports chat history. Simply, set the enableChatHistory
flag to true
and provide an a chat history store. For testing, you may use the in-memory chat history store.
// Close-ended chat endpoint with support for chat history
defineChatEndpoint({
endpoint: "chat-close-history",
agentType: "close-ended",
topic: "Firebase",
enableChatHistory: true,
chatHistoryStore: new InMemoryChatHistoryStore(),
});
Input Schema
Expects an object with the following properties:
-
query
: User query to chat agent. -
chatId
: Optional chat ID to continue the chat history. If not provided, a new chat history will be created, and the chat ID for this chat history will be returned with the response. This chat ID can be sent in further requests to continue a specific conversation. If the provided chat ID is not valid or not found, as error is returned.{ "query": "string", "chatId": "string" }
Output Schema
For successful requests, the response will contain:
-
response
: Chat agent response to the user query, returned as a string by default. (You may add more endpoints or update existing ones undersrc/endpoints
) -
chatId
: Chat ID for the current chat history. This can be used to continue the chat history in further requests.{ "response": "string", "chatId": "string" }
For failed requests, the response will contain:
-
error
: Error message returned by the server.{ "error": "string" }
Testing
Can use commands below to test the endpoint defined in the example above.
curl -X POST "http://127.0.0.1:3400/chat-close-history" -H "Content-Type: application/json" -d '{"data": { "query": "What is App check?" } }'
curl -X POST "http://127.0.0.1:3400/chat-close-history" -H "Content-Type: application/json" -d '{"data": { "query": "By using this, can you block traffic that does not have valid credentials?", "chatId": "<enter chat id here>" } }'
Genkit Developer UI
You could also start the Genkit Developer UI to test above:
npx genkit start
Endpoint to test: chat-close-history
Close Chat with Chat History, Caching, and Authentication
Restricted chat to a specific topic with chat history support, response caching, and API key authentication.
Usage
Below example displays how you can define a close-ended chat endpoint that supports chat history, response caching, and API key authentication.
The example below uses in-memory stores for chat history, caching and API key Storage, but you can easily provide your own stores when configuring the endpoint.
// Initialize a Test API key store
const testAPIKeyStore = new InMemoryAPIKeyStore();
// add a test API key
const key = "test-api-key";
testAPIKeyStore.addKey(key, {
uid: "test-user",
status: "active",
endpoints: "all", // allow access to all endpoints
});
// Close-ended chat endpoint with support for chat history, authentication, and caching
defineChatEndpoint({
agentType: "close-ended",
topic: "Firebase",
endpoint: "chat-close-history-auth-cached",
enableChatHistory: true,
chatHistoryStore: new InMemoryChatHistoryStore(),
enableAuth: true,
apiKeyStore: testAPIKeyStore,
enableCache: true,
cacheStore: new InMemoryCacheStore(),
});
Input Schema
Request header requires an API key for authentication:
key
: API key for authentication. The API key must be owned by the user making the request, and should have authorization to access the endpoint endpoint.
In request data, expects an object with the following properties:
-
query
: User query to chat agent. -
uid
: User ID of the user making the query. Required to assess if user is authorized to access the endpoint endpoint. The API key provided in headers must be owned by this user. -
chatId
: Optional chat ID to continue the chat history. If not provided, a new chat history will be created, and the chat ID for this chat history will be returned with the response. This chat ID can be sent in further requests to continue a specific conversation. If the provided chat ID is not valid or not found, as error is returned.{ "query": "string", "uid": "string", "chatId": "string" }
Output Schema
For successful requests, the response will contain:
-
response
: Chat agent response to the user query, returned as a string by default. (You may add more endpoints or update existing ones undersrc/endpoints
) -
chatId
: Chat ID for the current chat history. This can be used to continue the chat history in further requests.{ "response": "string", "chatId": "string" }
For failed requests, the response will contain:
-
error
: Error message returned by the server.{ "error": "string" }
Testing
Can use commands below to test the endpoint defined in the example above.
On running the first example, you should receive an object as a response with the chat ID for the chat history. Add this chat ID to the second example to continue the chat history.
curl -X POST "http://127.0.0.1:3400/chat-close-history-auth-cached" -H "Content-Type: application/json" -H "Authorization: test-api-key" -d '{"data": { "query": "What is App check?", "uid": "test-user" } }'
curl -X POST "http://127.0.0.1:3400/chat-close-history-auth-cached" -H "Content-Type: application/json" -H "Authorization: test-api-key" -d '{"data": { "query": "By using this, can you block traffic that does not have valid credentials?", "uid": "test-user", "chatId": "<enter chat id here>" } }'
Genkit Developer UI
You could also start the Genkit Developer UI to test above:
npx genkit start
Endpoint to test: chat-close-history-auth-cached
Configurations
As you can see from above, its quite easy to turn features on and off. For all chat endpoint configurations, please check Chat Endpoint Configurations.