Skip to content

Commit

Permalink
upgrade cohere (#15)
Browse files Browse the repository at this point in the history
* upgrade cohere

* fix with tsconfig changes
  • Loading branch information
jrhizor authored Nov 6, 2023
1 parent 6c5955d commit 524ff14
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 7 additions & 4 deletions src/elelem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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.`,
Expand Down
19 changes: 10 additions & 9 deletions src/elelem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -101,10 +102,10 @@ const callOpenAIApi = async (
};

const callCohereApi = async (
cohere: Cohere,
cohere: CohereClient,
systemPromptWithFormat: string,
userPrompt: string,
modelOptions: Omit<generateRequest, "prompt"> & { max_tokens: number },
modelOptions: Omit<GenerateRequest, "prompt"> & { max_tokens: number },
): Promise<string> => {
return await getTracer().startActiveSpan(`cohere-call`, async (span) => {
span.setAttribute("cohere.prompt.system", systemPromptWithFormat);
Expand All @@ -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;
});
};

Expand Down Expand Up @@ -447,7 +448,7 @@ export const elelem: Elelem = {
const apiCaller = async (
systemPromptWithFormat: string,
userPrompt: string,
combinedOptions: Omit<generateRequest, "prompt"> & {
combinedOptions: Omit<GenerateRequest, "prompt"> & {
max_tokens: number;
},
): Promise<string> => {
Expand All @@ -459,7 +460,7 @@ export const elelem: Elelem = {
);
};

const combinedOptions: Omit<generateRequest, "prompt"> & {
const combinedOptions: Omit<GenerateRequest, "prompt"> & {
max_tokens: number;
} = {
...{ max_tokens: 100 },
Expand Down
19 changes: 8 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,16 +27,16 @@ export interface CohereGenerateBaseConfig {

export interface Cohere {
generate: (
config: generateRequest,
) => Promise<cohereResponse<generateResponse>>;
config: GenerateRequest,
) => Promise<GenerationFinalResponse>;
}

export interface ElelemConfig {
// only applies to generations, not cache retries, which always use the default behavior
backoffOptions?: BackoffOptions;
cache?: ElelemCacheConfig;
openai?: OpenAI;
cohere?: Cohere;
cohere?: CohereClient;
}

export interface Elelem {
Expand All @@ -50,12 +47,12 @@ export type ElelemFormatter = <T>(schema: ZodType<T>) => string;

export interface ElelemModelOptions {
openai?: Omit<ChatCompletionCreateParamsNonStreaming, "messages">;
cohere?: Partial<Omit<generateRequest, "prompt">>;
cohere?: Partial<Omit<GenerateRequest, "prompt">>;
}

export interface PartialElelemModelOptions {
openai?: Partial<Omit<ChatCompletionCreateParamsNonStreaming, "messages">>;
cohere?: Partial<Omit<generateRequest, "prompt">>;
cohere?: Partial<Omit<GenerateRequest, "prompt">>;
}

export interface InitializedElelem {
Expand All @@ -80,7 +77,7 @@ export interface ElelemContext {

cohere: <T>(
chatId: string,
modelOptions: Partial<Omit<generateRequest, "prompt">>,
modelOptions: Partial<Omit<GenerateRequest, "prompt">>,
systemPrompt: string,
userPrompt: string,
schema: ZodType<T>,
Expand Down
18 changes: 12 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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": []
}
56 changes: 52 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 524ff14

Please sign in to comment.