diff --git a/package.json b/package.json index bf9ffd9..e89edd0 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "@faker-js/faker": "^8.2.0", "@opentelemetry/api": "^1.6.0", "@types/object-hash": "^3.0.5", - "cohere-ai": "^6.2.2", + "cohere-ai": "^7.1.1", "dotenv": "^16.3.1", "exponential-backoff": "^3.1.1", "ioredis": "^5.3.2", diff --git a/src/elelem.test.ts b/src/elelem.test.ts index e476610..9a24fe6 100644 --- a/src/elelem.test.ts +++ b/src/elelem.test.ts @@ -4,7 +4,7 @@ import { z } from "zod"; import { elelem } from "./elelem"; import { describe, expect, test, afterAll } from "@jest/globals"; import { config } from "dotenv"; -import cohere from "cohere-ai"; +import { CohereClient } from "cohere-ai"; import { ElelemUsage, ElelemError } from "./types"; import * as opentelemetry from "@opentelemetry/sdk-node"; @@ -28,7 +28,10 @@ config(); const redisClient = new Redis(process.env.REDIS!); const openAiClient = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); -cohere.init(process.env.COHERE_API_KEY || ""); + +const cohere = new CohereClient({ + token: process.env.COHERE_API_KEY || "", +}); const llm = elelem.init({ openai: openAiClient, @@ -351,7 +354,7 @@ describe("cohere", () => { async (c) => { const { result: capitol } = await c.cohere( "capitol", - { max_tokens: 100, temperature: 0 }, + { maxTokens: 100, temperature: 0 }, `What is the capitol of the country provided?`, "USA", capitolResponseSchema, @@ -362,7 +365,7 @@ describe("cohere", () => { const { result: cityDescription } = await c.cohere( "city-description", { - max_tokens: 100, + maxTokens: 100, temperature: 0, }, `For the given capitol city, return the founding year and an estimate of the population of the city.`, diff --git a/src/elelem.ts b/src/elelem.ts index 82e4889..7324562 100644 --- a/src/elelem.ts +++ b/src/elelem.ts @@ -27,7 +27,8 @@ import { getCache } from "./caching"; import { setElelemConfigAttributes, setUsageAttributes } from "./tracing"; import { ChatCompletionCreateParamsNonStreaming } from "openai/resources/chat/completions"; import { ZodType } from "zod"; -import { generateRequest } from "cohere-ai/dist/models"; +import {GenerateRequest} from "cohere-ai/api"; +import {CohereClient} from "cohere-ai"; function getTracer() { return trace.getTracer("elelem", "0.0.1"); @@ -101,10 +102,10 @@ const callOpenAIApi = async ( }; const callCohereApi = async ( - cohere: Cohere, + cohere: CohereClient, systemPromptWithFormat: string, userPrompt: string, - modelOptions: Omit & { max_tokens: number }, + modelOptions: Omit & { max_tokens: number }, ): Promise => { return await getTracer().startActiveSpan(`cohere-call`, async (span) => { span.setAttribute("cohere.prompt.system", systemPromptWithFormat); @@ -115,17 +116,17 @@ const callCohereApi = async ( prompt: `${systemPromptWithFormat}\n${userPrompt}`, }); - if (response.statusCode !== 200) { + if (response.generations.length === 0) { span.end(); - throw new Error("Error code from api!"); + throw new Error("No generation from API!"); } // todo: add cost calculation once available if cohere supports it in the future - span.setAttribute("cohere.response", response.body.generations[0].text); + span.setAttribute("cohere.response", response.generations[0].text); span.end(); - return response.body.generations[0].text; + return response.generations[0].text; }); }; @@ -447,7 +448,7 @@ export const elelem: Elelem = { const apiCaller = async ( systemPromptWithFormat: string, userPrompt: string, - combinedOptions: Omit & { + combinedOptions: Omit & { max_tokens: number; }, ): Promise => { @@ -459,7 +460,7 @@ export const elelem: Elelem = { ); }; - const combinedOptions: Omit & { + const combinedOptions: Omit & { max_tokens: number; } = { ...{ max_tokens: 100 }, diff --git a/src/types.ts b/src/types.ts index 6d1351b..ce04ab8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,11 +5,8 @@ import { ZodType } from "zod"; import { ChatCompletionCreateParamsNonStreaming } from "openai/resources/chat/completions"; import { CompletionUsage } from "openai/resources"; import { Span } from "@opentelemetry/api"; -import { - cohereResponse, - generateRequest, - generateResponse, -} from "cohere-ai/dist/models"; +import {GenerateRequest, GenerationFinalResponse} from "cohere-ai/api"; +import {CohereClient} from "cohere-ai"; export interface ElelemCache { // keys will be hashed using object-hash @@ -30,8 +27,8 @@ export interface CohereGenerateBaseConfig { export interface Cohere { generate: ( - config: generateRequest, - ) => Promise>; + config: GenerateRequest, + ) => Promise; } export interface ElelemConfig { @@ -39,7 +36,7 @@ export interface ElelemConfig { backoffOptions?: BackoffOptions; cache?: ElelemCacheConfig; openai?: OpenAI; - cohere?: Cohere; + cohere?: CohereClient; } export interface Elelem { @@ -50,12 +47,12 @@ export type ElelemFormatter = (schema: ZodType) => string; export interface ElelemModelOptions { openai?: Omit; - cohere?: Partial>; + cohere?: Partial>; } export interface PartialElelemModelOptions { openai?: Partial>; - cohere?: Partial>; + cohere?: Partial>; } export interface InitializedElelem { @@ -80,7 +77,7 @@ export interface ElelemContext { cohere: ( chatId: string, - modelOptions: Partial>, + modelOptions: Partial>, systemPrompt: string, userPrompt: string, schema: ZodType, diff --git a/tsconfig.json b/tsconfig.json index 3d62886..fb38543 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,20 @@ { "compilerOptions": { + "extendedDiagnostics": true, "strict": true, - "incremental": true, + "target": "ES6", + "module": "CommonJS", + "moduleResolution": "node", "esModuleInterop": true, - "module": "commonjs", - "target": "es2019", + "skipLibCheck": true, "declaration": true, - "outDir": "./dist" + "noUnusedParameters": true, + "outDir": "dist", + "rootDir": "src", + "baseUrl": "src" }, "include": [ - "src/**/*" - ] + "src" + ], + "exclude": [] } diff --git a/yarn.lock b/yarn.lock index d86966e..aa0c3ca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -980,6 +980,11 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c" integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw== +"@types/url-join@4.0.1": + version "4.0.1" + resolved "https://registry.yarnpkg.com/@types/url-join/-/url-join-4.0.1.tgz#4989c97f969464647a8586c7252d97b449cdc045" + integrity sha512-wDXw9LEEUHyV+7UWy7U315nrJGJ7p1BzaCxDpEoLr789Dk1WDVMMlf3iBfbG2F8NdWnYyFbtTxUn2ZNbm1Q4LQ== + "@types/yargs-parser@*": version "21.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b" @@ -992,6 +997,11 @@ dependencies: "@types/yargs-parser" "*" +"@ungap/url-search-params@0.2.2": + version "0.2.2" + resolved "https://registry.yarnpkg.com/@ungap/url-search-params/-/url-search-params-0.2.2.tgz#2de3bdec21476a9b70ef11fd7b794752f9afa04c" + integrity sha512-qQsguKXZVKdCixOHX9jqnX/K/1HekPDpGKyEcXHT+zR6EjGA7S4boSuelL4uuPv6YfhN0n8c4UxW+v/Z3gM2iw== + abort-controller@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" @@ -1072,6 +1082,14 @@ asynckit@^0.4.0: resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== +axios@0.27.2: + version "0.27.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972" + integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ== + dependencies: + follow-redirects "^1.14.9" + form-data "^4.0.0" + babel-jest@^29.7.0: version "29.7.0" resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.7.0.tgz#f4369919225b684c56085998ac63dbd05be020d5" @@ -1272,10 +1290,16 @@ co@^4.6.0: resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" integrity sha512-QVb0dM5HvG+uaxitm8wONl7jltx8dqhfU33DcqtOZcLSVIKSDDLDi7+0LbAKiyI8hD9u42m2YxXSkMGWThaecQ== -cohere-ai@^6.2.2: - version "6.2.2" - resolved "https://registry.yarnpkg.com/cohere-ai/-/cohere-ai-6.2.2.tgz#ea3a01d5bad839ffb006e8ec793c034a951cca7e" - integrity sha512-+Tq+4e8N/YWKJqFpWaULsfbZR/GOvGh8WWYFKR1bpipu8bCok3VcbTPnBmIToQiIqOgFpGk3HsA4b0guVyL3vg== +cohere-ai@^7.1.1: + version "7.1.1" + resolved "https://registry.yarnpkg.com/cohere-ai/-/cohere-ai-7.1.1.tgz#a7f4ac583e1517b62971b224ac782fb5e0872048" + integrity sha512-yY8MkrRIhi/FHmDyqssjFpg0O4Sj8Jb/GsWCXEo+jIKjwUPeodaZwxGmP9TrouKqWh5g06EFOT4bF5VOJzSRSw== + dependencies: + "@types/url-join" "4.0.1" + "@ungap/url-search-params" "0.2.2" + axios "0.27.2" + js-base64 "3.7.2" + url-join "4.0.1" collect-v8-coverage@^1.0.0: version "1.0.2" @@ -1535,6 +1559,11 @@ find-up@^4.0.0, find-up@^4.1.0: locate-path "^5.0.0" path-exists "^4.0.0" +follow-redirects@^1.14.9: + version "1.15.3" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a" + integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q== + form-data-encoder@1.7.2: version "1.7.2" resolved "https://registry.yarnpkg.com/form-data-encoder/-/form-data-encoder-1.7.2.tgz#1f1ae3dccf58ed4690b86d87e4f57c654fbab040" @@ -1549,6 +1578,15 @@ form-data@^3.0.0: combined-stream "^1.0.8" mime-types "^2.1.12" +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + formdata-node@^4.3.2: version "4.4.1" resolved "https://registry.yarnpkg.com/formdata-node/-/formdata-node-4.4.1.tgz#23f6a5cb9cb55315912cbec4ff7b0f59bbd191e2" @@ -2173,6 +2211,11 @@ jest@^29.7.0: import-local "^3.0.2" jest-cli "^29.7.0" +js-base64@3.7.2: + version "3.7.2" + resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.2.tgz#816d11d81a8aff241603d19ce5761e13e41d7745" + integrity sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ== + js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -2854,6 +2897,11 @@ update-browserslist-db@^1.0.11: escalade "^3.1.1" picocolors "^1.0.0" +url-join@4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/url-join/-/url-join-4.0.1.tgz#b642e21a2646808ffa178c4c5fda39844e12cde7" + integrity sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA== + uuid@^8.3.2: version "8.3.2" resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"