From d9614eb93056400844f16227d64d41653cbede6f Mon Sep 17 00:00:00 2001 From: Alex Dixon Date: Sat, 12 Oct 2024 09:11:42 -0700 Subject: [PATCH] fix tests --- typescript/test/image.mocha.ts | 4 +- typescript/test/runtime.mocha.ts | 108 ++++++++++++++-------------- typescript/test/tools.mocha.ts | 118 ++++++++++++++++--------------- typescript/test/util.ts | 23 ++++++ 4 files changed, 141 insertions(+), 112 deletions(-) create mode 100644 typescript/test/util.ts diff --git a/typescript/test/image.mocha.ts b/typescript/test/image.mocha.ts index f79df8d3a..bd0ac113e 100644 --- a/typescript/test/image.mocha.ts +++ b/typescript/test/image.mocha.ts @@ -5,7 +5,7 @@ import assert from 'assert' test('detectImageFormatFromBase64', () => { // Test empty string - assert.equal(imageFormatFromBase64(''), 'unknown') + assert.equal(imageFormatFromBase64(''), null) // Test JPEG assert.equal(imageFormatFromBase64('/9j/4AAQSkZJRgABAQEAYABgAAD'), 'jpeg') @@ -29,5 +29,5 @@ test('detectImageFormatFromBase64', () => { assert.equal(imageFormatFromBase64('UklGRh4AAABXRUJQVlA4'), 'webp') // Test unknown format - assert.equal(imageFormatFromBase64('SGVsbG8gV29ybGQh'), 'unknown') + assert.equal(imageFormatFromBase64('SGVsbG8gV29ybGQh'), null) }) diff --git a/typescript/test/runtime.mocha.ts b/typescript/test/runtime.mocha.ts index bac3b1a8b..4e359e938 100644 --- a/typescript/test/runtime.mocha.ts +++ b/typescript/test/runtime.mocha.ts @@ -1,69 +1,71 @@ import * as logging from '../src/util/_logging' logging.setGlobalLevel(logging.LogLevel.DEBUG) -import { test, before } from 'mocha' +import { test, beforeEach } from 'mocha' import OpenAI from 'openai' import { config } from '../src/configurator' import { Message } from '../src/types' import { complex, simple } from 'ell-ai' import assert from 'assert' +import {chatCompletionsToStream} from "./util"; -// this runs like...everywhere. in every test -// before(() => { -// config.defaultClient = config.defaultClient || new OpenAI({ apiKey: 'test' }) -// // @ts-expect-error -// config.defaultClient.chat.completions.create = async (...args) => { -// return { -// usage: { -// prompt_tokens: 10, -// completion_tokens: 10, -// latency_ms: 10, -// total_tokens: 20, -// }, -// id: 'chatcmpl-123', -// created: 1677652288, -// model: 'gpt-3.5-turbo-0125', -// object: 'chat.completion', -// choices: [ -// { -// index: 0, -// finish_reason: 'stop', -// logprobs: null, -// message: { -// // @ts-expect-error -// content: args[0].messages[0].content[0].text, -// role: 'assistant', -// refusal: null, -// }, -// }, -// ], -// } -// } -// }) -const logger = logging.getLogger('runtime.test') -test('runtime', async () => { - logger.debug('runtime', { test: 'test' }) - const child = simple({ model: 'gpt-4o-mini' }, async (a: string) => { - return 'child' - }) - const hello = simple({ model: 'gpt-4o' }, async (a: { a: string }) => { - const ok = await child(a.a) - return a.a + ok + +describe('lmp', () => { + beforeEach(() => { + config.defaultClient = config.defaultClient || new OpenAI({ apiKey: 'test' }) + // @ts-expect-error + config.defaultClient.chat.completions.create = async (...args) => { + return chatCompletionsToStream([{ + usage: { + prompt_tokens: 10, + completion_tokens: 10, + latency_ms: 10, + total_tokens: 20, + }, + id: 'chatcmpl-123', + created: 1677652288, + model: 'gpt-3.5-turbo-0125', + object: 'chat.completion', + choices: [ + { + index: 0, + finish_reason: 'stop', + logprobs: null, + message: { + // @ts-expect-error + content: args[0].messages[0].content[0].text, + role: 'assistant', + refusal: null, + }, + }, + ], + }]) + } }) - const result = await hello({ a: 'world' }) + test('runtime', async () => { + const child = simple({ model: 'gpt-4o-mini' }, async (a: string) => { + return 'child' + }) + const hello = simple({ model: 'gpt-4o' }, async (a: { a: string }) => { + const ok = await child(a.a) + return a.a + ok + }) - assert.equal(result, 'worldchild') + const result = await hello({ a: 'world' }) - assert.ok(hello.__ell_lmp_id__?.startsWith('lmp-')) - assert.equal(hello.__ell_lmp_name__, 'hello') + assert.equal(result, 'worldchild') - assert.ok(child.__ell_lmp_id__?.startsWith('lmp-')) - assert.equal(child.__ell_lmp_name__, 'child') -}) + assert.ok(hello.__ell_lmp_id__?.startsWith('lmp-')) + assert.equal(hello.__ell_lmp_name__, 'hello') -test('complex', async () => { - const child2 = complex({ model: 'gpt-4o-mini' }, async (a: string) => [new Message('assistant', 'child')]) - const result = await child2('world') - assert.deepStrictEqual(result, [new Message('assistant', 'child')]) + assert.ok(child.__ell_lmp_id__?.startsWith('lmp-')) + assert.equal(child.__ell_lmp_name__, 'child') + }) + + test('complex', async () => { + const child2 = complex({ model: 'gpt-4o-mini' }, async (a: string) => [new Message('assistant', 'child')]) + const result = await child2('world') + assert.deepStrictEqual(result, new Message('assistant', 'child')) + }) }) diff --git a/typescript/test/tools.mocha.ts b/typescript/test/tools.mocha.ts index c4a854b52..5b20f9d9a 100644 --- a/typescript/test/tools.mocha.ts +++ b/typescript/test/tools.mocha.ts @@ -7,66 +7,70 @@ import { Message } from '../src/types' import { complex, tool } from 'ell-ai' import assert from 'assert' -before(() => { - config.defaultClient = config.defaultClient || new OpenAI({ apiKey: 'test' }) - // @ts-expect-error - config.defaultClient.chat.completions.create = async (...args) => { - return { - usage: { - prompt_tokens: 10, - completion_tokens: 10, - latency_ms: 10, - total_tokens: 20, - }, - id: 'chatcmpl-123', - created: 1677652288, - model: 'gpt-3.5-turbo-0125', - object: 'chat.completion', - choices: [ - { - index: 0, - finish_reason: 'tool_calls', - logprobs: null, - message: { - tool_calls: [ - { - type: 'function', - id: '123', - function: { name: 'getWeather', arguments: JSON.stringify({ place: 'santa cruz' }) }, - }, - ], - }, - }, - ], - } - } -}) -test('tools', async () => { - const getWeather = tool( - async ({ place }: { place: string }) => { - return `The weather in ${place} is pretty nice.` - }, - { - description: 'Get the weather in a given place', - paramDescriptions: { - place: 'The place to get the weather for', - }, + +describe("tools", () => { + before(() => { + config.defaultClient = config.defaultClient || new OpenAI({apiKey: 'test'}) + // @ts-expect-error + config.defaultClient.chat.completions.create = async (...args) => { + return { + usage: { + prompt_tokens: 10, + completion_tokens: 10, + latency_ms: 10, + total_tokens: 20, + }, + id: 'chatcmpl-123', + created: 1677652288, + model: 'gpt-3.5-turbo-0125', + object: 'chat.completion', + choices: [ + { + index: 0, + finish_reason: 'tool_calls', + logprobs: null, + message: { + tool_calls: [ + { + type: 'function', + id: '123', + function: {name: 'getWeather', arguments: JSON.stringify({place: 'santa cruz'})}, + }, + ], + }, + }, + ], + } } - ) - const hello = complex({ model: 'gpt-4o', tools: [getWeather] }, async (place: string) => { - return [new Message('user', `Can you tell me the weather in ${place}?`)] }) + test('getWeather', async () => { + const getWeather = tool( + async ({place}: { place: string }) => { + return `The weather in ${place} is pretty nice.` + }, + { + description: 'Get the weather in a given place', + paramDescriptions: { + place: 'The place to get the weather for', + }, + } + ) + const hello = complex({model: 'gpt-4o', tools: [getWeather]}, async (place: string) => { + return [new Message('user', `Can you tell me the weather in ${place}?`)] + }) + + const result = await hello('santa cruz') + assert.equal( + await result.callToolsAndCollectAsMessage().then((x) => x.toolResults?.[0]?.result.map((x) => x.text).join('')), + '"The weather in santa cruz is pretty nice."' + ) - const result = await hello('santa cruz') - assert.equal( - await result.callToolsAndCollectAsMessage().then((x) => x.toolResults?.[0]?.result.map((x) => x.text).join('')), - '"The weather in santa cruz is pretty nice."' - ) + // @ts-expect-error + assert.ok(getWeather.__ell_lmp_id__?.startsWith('lmp-')) + // @ts-expect-error + assert.equal(getWeather.__ell_lmp_name__, 'getWeather') + }) - // @ts-expect-error - assert.ok(getWeather.__ell_lmp_id__?.startsWith('lmp-')) - // @ts-expect-error - assert.equal(getWeather.__ell_lmp_name__, 'getWeather') -}) +}) \ No newline at end of file diff --git a/typescript/test/util.ts b/typescript/test/util.ts new file mode 100644 index 000000000..b7c85606c --- /dev/null +++ b/typescript/test/util.ts @@ -0,0 +1,23 @@ +import OpenAI from "openai"; + +export const chatCompletionsToStream = (completions: OpenAI.Chat.Completions.ChatCompletion[]) => { + return completions.map((completion):OpenAI.ChatCompletionChunk => { + return { + id: completion.id, + created: completion.created, + model: completion.model, + object: 'chat.completion.chunk', + choices: completion.choices.map((choice,i):OpenAI.ChatCompletionChunk.Choice => { + return { + delta: { + content: choice.message.content, + role: choice.message.role, + refusal: choice.message.refusal, + }, + index: choice.index || i, + finish_reason: choice.finish_reason, + } + }), + } + }) +} \ No newline at end of file