diff --git a/examples/basic.ts b/examples/basic.ts index 2bc42e1..399ee42 100644 --- a/examples/basic.ts +++ b/examples/basic.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/examples/expert-question-answering.ts b/examples/expert-question-answering.ts index c8f9a51..56e91c8 100644 --- a/examples/expert-question-answering.ts +++ b/examples/expert-question-answering.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { diff --git a/examples/facts.ts b/examples/facts.ts index 9ae9b34..0c308c0 100644 --- a/examples/facts.ts +++ b/examples/facts.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/examples/hf0-demo.ts b/examples/hf0-demo.ts index 73f3807..1ee1e24 100644 --- a/examples/hf0-demo.ts +++ b/examples/hf0-demo.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { diff --git a/examples/search-and-crawl.ts b/examples/search-and-crawl.ts index e33e607..4f0a071 100644 --- a/examples/search-and-crawl.ts +++ b/examples/search-and-crawl.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic, SearchAndCrawlTool, WeatherTool } from '@/index' @@ -30,7 +30,7 @@ async function main() { topic: z.string() }) ) - .call({ topic }) + .callWithMetadata({ topic }) console.log('\n\n\n') console.log(res) diff --git a/examples/sentiment.ts b/examples/sentiment.ts index 74576a5..de49ddb 100644 --- a/examples/sentiment.ts +++ b/examples/sentiment.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/examples/summarize-news.ts b/examples/summarize-news.ts index 8ba6a89..55ebe0d 100644 --- a/examples/summarize-news.ts +++ b/examples/summarize-news.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic, SerpAPITool } from '@/index' diff --git a/examples/temperature.ts b/examples/temperature.ts index 39dcdcf..6232195 100644 --- a/examples/temperature.ts +++ b/examples/temperature.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic, CalculatorTool, WeatherTool } from '@/index' diff --git a/package.json b/package.json index 7af5665..79ef0ab 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "test:eslint": "eslint \"**/*.ts\"" }, "dependencies": { - "@agentic/openai-fetch": "^1.5.3", "@anthropic-ai/sdk": "^0.4.4", "@inquirer/checkbox": "^1.3.1", "@inquirer/editor": "^1.2.0", @@ -54,6 +53,7 @@ "ky": "^0.33.3", "nanoid": "^4.0.2", "normalize-url": "^8.0.0", + "openai-fetch": "^1.6.3", "p-map": "^6.0.0", "p-retry": "^5.1.2", "p-throttle": "^5.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5369e2e..81f7afc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,13 +1,10 @@ -lockfileVersion: '6.1' +lockfileVersion: '6.0' settings: autoInstallPeers: true excludeLinksFromLockfile: false dependencies: - '@agentic/openai-fetch': - specifier: ^1.5.3 - version: 1.5.3 '@anthropic-ai/sdk': specifier: ^0.4.4 version: 0.4.4 @@ -53,6 +50,9 @@ dependencies: normalize-url: specifier: ^8.0.0 version: 8.0.0 + openai-fetch: + specifier: ^1.6.3 + version: 1.6.3 p-map: specifier: ^6.0.0 version: 6.0.0 @@ -178,13 +178,6 @@ devDependencies: packages: - /@agentic/openai-fetch@1.5.3: - resolution: {integrity: sha512-4c5YWz6jQdGxxM+SVhf0XW3mKYnFr56hntPep+y7wRfkjUl6lgZiuU3J61esQ8bj8vSFkgSfwjf3DeZIi/IEsg==} - dependencies: - ky: 0.33.3 - zod: 3.21.4 - dev: false - /@anthropic-ai/sdk@0.4.4: resolution: {integrity: sha512-Z/39nQi1sSUCeLII3lsAbL1u+0JF6cR2XmUEX9sLH0VtxmIjY6cjOUYjCkYh4oapTxOkhAFnVSAFJ6cxml2qXg==} dependencies: @@ -3322,6 +3315,13 @@ packages: mimic-fn: 4.0.0 dev: true + /openai-fetch@1.6.3: + resolution: {integrity: sha512-IM8bygs5qDOZ/WWB5nEPfm/JOqIj1BIO6R3gAW9vlL1oHtqO9D8ODQvkUNwMWRvW9xqqjlw4AnHYdhQ0o3NabA==} + dependencies: + ky: 0.33.3 + zod: 3.21.4 + dev: false + /optionator@0.9.1: resolution: {integrity: sha512-74RlY5FCnhq4jRxVUPKDaRwrVNXMqsGsiW6AJw4XK8hmtm10wC0ypZBLw5IIp85NZMr91+qd1RvvENwg7jjRFw==} engines: {node: '>= 0.8.0'} diff --git a/scratch/examples/calc-eval.ts b/scratch/examples/calc-eval.ts index dfdf676..5a73012 100644 --- a/scratch/examples/calc-eval.ts +++ b/scratch/examples/calc-eval.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/scratch/examples/equation-producer.ts b/scratch/examples/equation-producer.ts index 64499fb..4995a11 100644 --- a/scratch/examples/equation-producer.ts +++ b/scratch/examples/equation-producer.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/scratch/examples/food-expert.ts b/scratch/examples/food-expert.ts index bd67c8b..cc53b5a 100644 --- a/scratch/examples/food-expert.ts +++ b/scratch/examples/food-expert.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/scratch/examples/human-feedback-cli.ts b/scratch/examples/human-feedback-cli.ts index 27f64b1..56312d7 100644 --- a/scratch/examples/human-feedback-cli.ts +++ b/scratch/examples/human-feedback-cli.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { HumanFeedbackMechanismCLI } from '@/human-feedback' diff --git a/scratch/examples/human-feedback-slack.ts b/scratch/examples/human-feedback-slack.ts index b4a9bcb..57444b9 100644 --- a/scratch/examples/human-feedback-slack.ts +++ b/scratch/examples/human-feedback-slack.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { HumanFeedbackMechanismSlack } from '@/human-feedback' diff --git a/scratch/examples/human-feedback-twilio.ts b/scratch/examples/human-feedback-twilio.ts index eac976d..ec80b80 100644 --- a/scratch/examples/human-feedback-twilio.ts +++ b/scratch/examples/human-feedback-twilio.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { HumanFeedbackMechanismTwilio } from '@/human-feedback' diff --git a/scratch/examples/json-summary.ts b/scratch/examples/json-summary.ts index b98a30f..de68bdc 100644 --- a/scratch/examples/json-summary.ts +++ b/scratch/examples/json-summary.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/scratch/examples/misc.ts b/scratch/examples/misc.ts index 672f70f..e227b06 100644 --- a/scratch/examples/misc.ts +++ b/scratch/examples/misc.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic } from '@/agentic' diff --git a/scratch/examples/tools.ts b/scratch/examples/tools.ts index 0a00c2c..e64716b 100644 --- a/scratch/examples/tools.ts +++ b/scratch/examples/tools.ts @@ -1,5 +1,5 @@ -import { OpenAIClient } from '@agentic/openai-fetch' import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' import { z } from 'zod' import { Agentic, MetaphorSearchTool } from '@/index' diff --git a/src/llms/chat.ts b/src/llms/chat.ts index bc87100..b9e1f72 100644 --- a/src/llms/chat.ts +++ b/src/llms/chat.ts @@ -280,21 +280,25 @@ export abstract class BaseChatCompletion< functionCall.name = functionName } + const rawFunctionCallArguments = functionCall.arguments?.trim() let functionArguments: any - try { - functionArguments = JSON.parse(jsonrepair(functionCall.arguments)) - } catch (err: any) { - if (err instanceof JSONRepairError) { - throw new errors.OutputValidationError(err.message, { - cause: err - }) - } else if (err instanceof SyntaxError) { - throw new errors.OutputValidationError( - `Invalid JSON object: ${err.message}`, - { cause: err } - ) - } else { - throw err + + if (rawFunctionCallArguments) { + try { + functionArguments = JSON.parse(jsonrepair(rawFunctionCallArguments)) + } catch (err: any) { + if (err instanceof JSONRepairError) { + throw new errors.OutputValidationError(err.message, { + cause: err + }) + } else if (err instanceof SyntaxError) { + throw new errors.OutputValidationError( + `Invalid JSON object: ${err.message}`, + { cause: err } + ) + } else { + throw err + } } } diff --git a/src/llms/llm-utils.ts b/src/llms/llm-utils.ts index e4af27f..debab99 100644 --- a/src/llms/llm-utils.ts +++ b/src/llms/llm-utils.ts @@ -49,7 +49,7 @@ export async function getNumTokensForChatMessages({ let content = message.content || '' if (message.function_call) { // TODO: this case needs testing - content = message.function_call.arguments + content = message.function_call.arguments || '' } const [numTokensContent, numTokensRole, numTokensName] = diff --git a/src/types.ts b/src/types.ts index 9dc2147..dfa6d8c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ -import * as openai from '@agentic/openai-fetch' import * as anthropic from '@anthropic-ai/sdk' +import * as openai from 'openai-fetch' import ky from 'ky' import type { Options as RetryOptions } from 'p-retry' import type { JsonObject, Jsonifiable } from 'type-fest' diff --git a/test/_utils.ts b/test/_utils.ts index 5c34e32..2565f20 100644 --- a/test/_utils.ts +++ b/test/_utils.ts @@ -1,11 +1,11 @@ import * as anthropic from '@anthropic-ai/sdk' -import { OpenAIClient } from '@agentic/openai-fetch' import KeyvRedis from '@keyv/redis' import 'dotenv/config' import hashObject from 'hash-obj' import Redis from 'ioredis' import Keyv from 'keyv' import defaultKy, { AfterResponseHook, BeforeRequestHook } from 'ky' +import { OpenAIClient } from 'openai-fetch' import pMemoize from 'p-memoize' import * as types from '@/types'