-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf9c5c5
commit d9614eb
Showing
4 changed files
with
141 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <OpenAI.Chat.Completions.ChatCompletion>{ | ||
// 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: [ | ||
// <OpenAI.Chat.Completions.ChatCompletion.Choice>{ | ||
// 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([<OpenAI.Chat.Completions.ChatCompletion>{ | ||
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: [ | ||
<OpenAI.Chat.Completions.ChatCompletion.Choice>{ | ||
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')) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
}), | ||
} | ||
}) | ||
} |