-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ai & provider readmes, add getCountryTool file
- Loading branch information
Showing
5 changed files
with
224 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,60 @@ | ||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). | ||
# Chat App with EdgeDB AI and Vercel AI SDK | ||
|
||
This is an example project built with [Next.js](https://nextjs.org/) to showcase the use of [EdgeDB's AI](https://docs.edgedb.com/ai) features. The application demonstrates a chat app that allows users to query a digital library of imaginary books. Users can ask about books, their authors, and related details, leveraging the EdgeDB database to store and retrieve the data, and embeddings. For the LLM models, you can use any of the `OpenAI`, `Mistral`, or `Anthropic` models that EdgeDB AI supports. | ||
|
||
## Getting Started | ||
|
||
First, run the development server: | ||
- Install dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
- Initialize EdgeDB project: | ||
|
||
```bash | ||
edgedb project init | ||
edgedb migrate | ||
``` | ||
|
||
Check the schema file to see how the **deferred semantic similarity index** is defined for the Book type. This index enables the generation of embeddings for a provided expression, which are then used for retrieving relevant context from the database when users ask questions. | ||
|
||
- Seed the database with books and authors: | ||
|
||
```bash | ||
npm run seed | ||
``` | ||
|
||
- Start the development server: | ||
|
||
```bash | ||
npm run dev | ||
``` | ||
|
||
## Features | ||
|
||
### Chat Route (/) | ||
|
||
Users can have a conversation with EdgeDB AI about books. The conversation history is preserved. | ||
Some example questions: | ||
|
||
- What Ariadne writes about? | ||
- Where is she from? | ||
- What is the book "Whispers of the Forgotten City" about? | ||
|
||
### Completion route (/completion) | ||
|
||
A standalone query without persistent chat history. Each question is independent. | ||
|
||
### Function calling | ||
|
||
EdgeDB AI extension supports function calling. In this project we defined `getCountry` tool which is utilized in both the chat and completion routes. | ||
This tool retrieves an author's country of origin from the database. For example, if a user asks, **"Where is Ariadne from?"**, the getCountry tool should be invoked. | ||
The `processStream` function is responsible for parsing response chunks. When a tool call response is detected, the function executes the corresponding tool, updates the messages array with the tool's results, and provides the updated array back to the AI. | ||
|
||
**NOTE**: It is advisable to create a system query in a way that ensures it is aware of the available tools and understands when to call each tool. We achieved this in the seed script by updating the `builtin::rag-default` prompt. However, you can also update this prompt using the EdgeDB UI or via the REPL. Additionally, you can create a new prompt for this purpose using the UI or the REPL with an `INSERT` query. | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
# or | ||
bun dev | ||
``` | ||
### Feedback and Contributions | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
Feel free to fork this project, suggest improvements, or raise issues. | ||
This project is a simple starting point for exploring how EdgeDB can integrate with Vercel AI SDK. |
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,69 @@ | ||
import { EdgeDBAssistantMessage, EdgeDBToolMessage } from "@edgedb/ai"; | ||
import type { Client } from "edgedb"; | ||
|
||
export const countryTool = { | ||
type: "function", | ||
function: { | ||
name: "getCountry", | ||
description: "Get the country of the author", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
author: { | ||
type: "string", | ||
description: "Author name to get the country for.", | ||
}, | ||
}, | ||
required: ["author"], | ||
}, | ||
}, | ||
}; | ||
|
||
export async function getCountry(client: Client, author: string) { | ||
const res: { name: string; country: string } | null = | ||
await client.querySingle( | ||
` | ||
select Author { name, country } | ||
filter .name=<str>$author;`, | ||
{ author } | ||
); | ||
|
||
return res?.country | ||
? res | ||
: { | ||
...res, | ||
country: `There is no available data on the country of origin for ${author}.`, | ||
}; | ||
} | ||
|
||
export function generateToolMessages(toolCalls: any[], results: any[]) { | ||
let messages: (EdgeDBAssistantMessage | EdgeDBToolMessage)[] = []; | ||
|
||
toolCalls.forEach((call, i) => { | ||
messages.push( | ||
...[ | ||
{ | ||
role: "assistant" as const, | ||
content: "", | ||
tool_calls: [ | ||
{ | ||
id: call.id, | ||
type: "function" as const, | ||
function: { | ||
name: call.name, | ||
arguments: call.args, | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
role: "tool" as const, | ||
content: JSON.stringify(results[i]), | ||
tool_call_id: call.id, | ||
}, | ||
] | ||
); | ||
}); | ||
|
||
return messages; | ||
} |
Oops, something went wrong.