diff --git a/.env b/.env index 768c8af..53cbd72 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ -VITE_OPENAI_API_KEY='' -VITE_OPENAI_API_BASEURL='' +OPENAI_API_BASEURL='' +OPENAI_API_KEY='' +SERPAPI_API_KEY='' diff --git a/packages/neuri/src/openai/agent.ts b/packages/neuri/src/openai/agent.ts index d29e0b0..9e87117 100644 --- a/packages/neuri/src/openai/agent.ts +++ b/packages/neuri/src/openai/agent.ts @@ -2,8 +2,8 @@ import type { CommonProviderOptions } from '@xsai/providers' import type { Message } from '@xsai/shared-chat' import type { ChatCompletion, DefinedTool } from './types' -import { chatCompletion } from '@xsai/shared-chat' import { chatCompletionFromOpenAIChatCompletion, resolveFirstToolCallFromCompletion } from './completion' +import { generate } from './generate' import { invokeFunctionWithTools } from './invoke' import { assistant, tool } from './messages' import { tools } from './tools' @@ -17,7 +17,7 @@ export function composeAgent(options: { while (max >= 0) { max-- - const reqRes = await chatCompletion({ + const res = await generate({ apiKey: options.provider.apiKey, baseURL: options.provider.baseURL, model: callOptions.model, @@ -25,8 +25,6 @@ export function composeAgent(options: { tools: tools(options.tools), }) - const res = await reqRes.json() as ChatCompletion - const resChatCompletionToolCall = resolveFirstToolCallFromCompletion(res) if (!resChatCompletionToolCall) return res diff --git a/packages/neuri/src/openai/generate.ts b/packages/neuri/src/openai/generate.ts new file mode 100644 index 0000000..cec374c --- /dev/null +++ b/packages/neuri/src/openai/generate.ts @@ -0,0 +1,14 @@ +import type { GenerateTextOptions } from '@xsai/generate-text' +import type { ChatCompletion } from './types' +import { chatCompletion } from '@xsai/shared-chat' + +/** + * Generate text + * + * @param params - The parameters to generate text. + * @returns ChatCompletion + */ +export async function generate(params: GenerateTextOptions) { + const reqRes = await chatCompletion(params) + return await reqRes.json() as ChatCompletion +} diff --git a/packages/neuri/src/openai/stream.ts b/packages/neuri/src/openai/stream.ts index 82b547d..61f5d85 100644 --- a/packages/neuri/src/openai/stream.ts +++ b/packages/neuri/src/openai/stream.ts @@ -71,8 +71,6 @@ async function* asyncIteratorFromReadableStream(res: Readable * Stream the completion response from LLM API. * * @param params - The parameters to stream the completion response. - * @param params.provider - The provider options. - * @param params.options - The options to create the completion. * @returns The completion response stream. */ export async function stream(params: StreamTextOptions): Promise {