-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ai rag provider #1129
Merged
Merged
Add ai rag provider #1129
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1e94601
add ai rag provider meant to work with vercel ai sdk
diksipav cc676af
Add readme for the edgedb-rag-provider
diksipav 660a5c2
Rename @edgedb/rag-sdk to @edgedb/vercel-ai-provider
diksipav 9dcf152
Lock ai-sdk versions
diksipav 45b7293
Use default prompt in provider doStream & doGenerate
diksipav a230f1e
Rename rag stuff into vercel-ai-provider stuff
diksipav b51476b
revert Deno lockfile version to 3
diksipav 08df0b9
Update readmes for the ai and the vercel provider libs
diksipav 4941fe5
Update Request type in the ai binding
diksipav f65bf4e
Update Request type in the ai binding
diksipav 37d5373
Always include messages in the AI prompt's custom field
diksipav 43b26d4
Update the err we send from the ai binding to the client
diksipav c34c0cd
Use client.resolveConnectionParams to get config
diksipav 1c228f3
Update ai binding readme
diksipav a52447a
ai lib & provider: fix prompt we send to ai ext
diksipav bb190df
Support for vercel ai sdk v4
diksipav ed08833
Extract an err properly and throw in ai lib
diksipav cc88937
Refactor ai lib and ai provider
diksipav 4447820
Merge master
diksipav 3421567
Use ts types in ai lib for chunk types instead of zod schema
diksipav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,43 @@ | ||
# AI SDK - EdgeDB Provider | ||
|
||
The provider for the [Vercel AI SDK](https://sdk.vercel.ai/docs) contains language model support for | ||
the OpenAi, Mistral and Anthropic chat and completion APIs that implements EdgeDB RAG and embedding model support for the OpenAI and Mistral embeddings API. | ||
|
||
## Setup | ||
|
||
The EdgeDB provider is available in the `@edgedb/ai-sdk` module. You can install it with: | ||
|
||
```bash | ||
npm i @edgedb/ai-sdk | ||
``` | ||
|
||
## Provider Instance | ||
|
||
You can import the default provider instance `edgedbRag` from `@edgedb/ai-sdk`: | ||
|
||
```ts | ||
import { edgedbRag } from "@edgedb/ai-sdk"; | ||
``` | ||
|
||
## Example | ||
|
||
```ts | ||
import { generateText } from "ai"; | ||
import { createClient } from "edgedb"; | ||
import { edgedbRag } from "@edgedb/ai-sdk"; | ||
|
||
const textModel = (await edgedbRag).languageModel("gpt-4-turbo-preview"); | ||
|
||
const { text } = await generateText({ | ||
model: textModel.withSettings({ | ||
context: { query: "your context" }, | ||
}), | ||
prompt: "your prompt", | ||
}); | ||
|
||
console.log(text); | ||
``` | ||
|
||
## Documentation | ||
|
||
Please check out the **[EdgeDB provider documentation](https://docs.edgedb.com)** for more information. |
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,60 @@ | ||
{ | ||
"name": "@edgedb/rag-sdk", | ||
diksipav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"version": "0.0.1", | ||
"license": "Apache-2.0", | ||
"sideEffects": false, | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"/dist" | ||
], | ||
"scripts": { | ||
"build": "tsc --project tsconfig.json", | ||
"clean": "rm -rf dist", | ||
"lint": "eslint \"./**/*.ts*\"", | ||
"type-check": "tsc --noEmit", | ||
"prettier-check": "prettier --check \"./**/*.ts*\"" | ||
}, | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"dependencies": { | ||
"@ai-sdk/provider": "0.0.24", | ||
"@ai-sdk/provider-utils": "^1.0.20" | ||
}, | ||
"devDependencies": { | ||
"@repo/tsconfig": "*", | ||
"@types/node": "^18", | ||
"tsup": "^8", | ||
"typescript": "5.5.4", | ||
"zod": "3.23.8", | ||
"edgedb": "*" | ||
}, | ||
"peerDependencies": { | ||
"zod": "^3.0.0", | ||
"edgedb": "^1.5.0" | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"homepage": "https://sdk.vercel.ai/docs", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vercel/ai.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/vercel/ai/issues" | ||
}, | ||
"keywords": [ | ||
"ai" | ||
] | ||
} |
93 changes: 93 additions & 0 deletions
93
packages/edgedb-rag-sdk/src/convert-to-edgedb-rag-messages.ts
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,93 @@ | ||
import { type LanguageModelV1Prompt } from "@ai-sdk/provider"; | ||
import type { EdgeDBRagMessage } from "./edgedb-rag-settings"; | ||
|
||
export function convertToEdgeDBRagMessages( | ||
prompt: LanguageModelV1Prompt, | ||
): EdgeDBRagMessage[] { | ||
const messages: EdgeDBRagMessage[] = []; | ||
|
||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
case "system": { | ||
messages.push({ role: "system", content }); | ||
break; | ||
} | ||
|
||
case "user": { | ||
messages.push({ | ||
role: "user", | ||
content: content.map((part) => { | ||
switch (part.type) { | ||
case "text": { | ||
return { type: "text", text: part.text }; | ||
} | ||
default: { | ||
throw new Error(`Unsupported type: ${part.type}`); | ||
} | ||
} | ||
}), | ||
}); | ||
break; | ||
} | ||
|
||
case "assistant": { | ||
let text = ""; | ||
const toolCalls: { | ||
id: string; | ||
type: "function"; | ||
function: { name: string; arguments: string }; | ||
}[] = []; | ||
|
||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
text += part.text; | ||
break; | ||
} | ||
case "tool-call": { | ||
toolCalls.push({ | ||
id: part.toolCallId, | ||
type: "function", | ||
function: { | ||
name: part.toolName, | ||
arguments: JSON.stringify(part.args), | ||
}, | ||
}); | ||
break; | ||
} | ||
|
||
default: { | ||
const _exhaustiveCheck: never = part; | ||
throw new Error(`Unsupported part: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
|
||
messages.push({ | ||
role: "assistant", | ||
content: text, | ||
tool_calls: toolCalls.length > 0 ? toolCalls : undefined, | ||
}); | ||
break; | ||
} | ||
|
||
case "tool": { | ||
for (const toolResponse of content) { | ||
messages.push({ | ||
role: "tool", | ||
content: JSON.stringify(toolResponse.result), | ||
tool_call_id: toolResponse.toolCallId, | ||
}); | ||
} | ||
break; | ||
} | ||
|
||
default: { | ||
const _exhaustiveCheck: never = role; | ||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
|
||
return messages; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not exporting this Zod schema or using it in this file: why are we using a Zod schema instead of writing this as a TypeScript interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used this in the vercel provider so it was easier to copy paste it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we export it then maybe? Not a blocker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can export it, I just dont understand/see when would someone need it?
we export:
export type StreamingMessage = z.infer<typeof _edgedbRagChunkSchema>;
which will user most probably use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I thought you mean that you are using a copy-pasted version of this schema in the Vercel provider, no? I'm saying export it here and import it in the Vercel provider instead of copy-pasting it. Or export it from the Vercel provider and import the type here. Whichever makes sense.
This is not a blocker for merging, it just feels a little strange here to have an essentially "private" Zod schema that isn't used for anything, but has to be kept in sync across two packages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm okay I can do this, even tho it makes sense and is better to have one source of truth it feels a bit weird to me to install the whole lib just because one type, but maybe that's usually the way to go in this kind of situations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I hadn't noticed: Does the Vercel AI SDK Provider not use the
@edgedb/ai
package for anything already?