-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adapt to the latest version of ai sdk
- Loading branch information
Showing
6 changed files
with
228 additions
and
256 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
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,63 +1,31 @@ | ||
import { ChatCompletionTool } from "openai/resources/chat/completions"; | ||
import { CoreTool, tool } from "ai"; | ||
import { z } from "zod"; | ||
|
||
interface ToolCall { | ||
tool: ChatCompletionTool; | ||
call: (name: string, args: Record<string, unknown>) => Promise<any>; | ||
} | ||
|
||
const toolMap: Record<string, ToolCall> = { | ||
googleSearch: { | ||
tool: { | ||
type: "function", | ||
function: { | ||
name: "googleSearch", | ||
description: "Search the web for information using Google", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
keyword: { | ||
type: "string", | ||
description: "Keywords for searching", | ||
}, | ||
}, | ||
required: ["keyword"], | ||
}, | ||
}, | ||
}, | ||
call: async (name, args) => { | ||
export const tools: Record<string, CoreTool> = { | ||
googleSearch: tool({ | ||
description: "Search the web for information using Google", | ||
parameters: z.object({ | ||
keyword: z.string().describe("Keywords for searching"), | ||
}), | ||
execute: async ({ keyword }) => { | ||
console.log(`- googleSearch: ${keyword}`); | ||
const nothing = "nothing"; | ||
const cfg = args.config as any; | ||
const apiKey = cfg.apiKey || process.env.GOOGLE_API_KEY; | ||
const engineId = cfg.engineId || process.env.GOOGLE_ENGINE_ID; | ||
const apiKey = process.env.GOOGLE_API_KEY; | ||
const engineId = process.env.GOOGLE_ENGINE_ID; | ||
if (!apiKey || !engineId) { | ||
console.log(`- ${name} apiKey or engineId is empty`); | ||
console.log("- googleSearch apiKey or engineId is empty"); | ||
return nothing; | ||
} | ||
try { | ||
const res = await fetch( | ||
`https://www.googleapis.com/customsearch/v1?&fields=items(title,link,snippet,pagemap/metatags(og:description))&key=${apiKey}&cx=${engineId}&q=${args.keyword}`, | ||
`https://www.googleapis.com/customsearch/v1?&fields=items(title,link,snippet,pagemap/metatags(og:description))&key=${apiKey}&cx=${engineId}&q=${keyword}`, | ||
); | ||
const result = await res.json(); | ||
return result.items ?? nothing; | ||
} catch (err: any) { | ||
console.log(`- ${name} ${err.cause ?? err}`); | ||
console.log(`- googleSearch: ${err.cause ?? err}`); | ||
return nothing; | ||
} | ||
}, | ||
}, | ||
}), | ||
}; | ||
|
||
export const tools: ChatCompletionTool[] = Object.values(toolMap).map( | ||
(value) => value.tool, | ||
); | ||
|
||
export async function onToolCall( | ||
name: string, | ||
args: Record<string, unknown>, | ||
): Promise<any> { | ||
console.log("- onToolCall", name, args); | ||
if (!toolMap.hasOwnProperty(name)) { | ||
return `${name} tool not found`; | ||
} | ||
return toolMap[name].call(name, args); | ||
} |
This file was deleted.
Oops, something went wrong.
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,12 +1,13 @@ | ||
import OpenAI from "openai"; | ||
import { createOpenAI, OpenAIProvider } from "@ai-sdk/openai"; | ||
|
||
const baseURL = process.env.OPENAI_BASE_URL; | ||
const OPENAI_API_KEY = process.env.OPENAI_API_KEY ?? ""; | ||
const clientPool: Map<string, OpenAI> = new Map(); | ||
const clientPool: Map<string, OpenAIProvider> = new Map(); | ||
|
||
export function getOpenAI(apiKey: string): OpenAI { | ||
export function getOpenAI(apiKey: string): OpenAIProvider { | ||
apiKey = apiKey || OPENAI_API_KEY; | ||
if (!clientPool.has(apiKey)) { | ||
clientPool.set(apiKey, new OpenAI({ apiKey })); | ||
clientPool.set(apiKey, createOpenAI({ apiKey, baseURL })); | ||
} | ||
return clientPool.get(apiKey)!; | ||
} |
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
Oops, something went wrong.