Skip to content

Willy/update vercel integration #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@literalai/client",
"version": "0.0.513",
"version": "0.0.514",
"description": "",
"exports": {
".": {
Expand Down
12 changes: 7 additions & 5 deletions src/instrumentation/vercel-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,14 @@ export const makeInstrumentVercelSDK = (
type TOptions = Options<TFunction>;
type TResult = Result<TFunction>;

return async (
options: TOptions & { literalAiParent?: Step | Thread }
): Promise<TResult> => {
const { literalAiParent: parent, ...originalOptions } = options;
return async (options: TOptions): Promise<TResult> => {
const startTime = Date.now();
const result: TResult = await (fn as any)(originalOptions);
const result: TResult = await (fn as any)(options);

const threadFromStore = client._currentThread();
const stepFromStore = client._currentStep();

const parent = stepFromStore || threadFromStore;

// Fork the stream to compute metrics
let stream: ReadableStream<OriginalStreamPart>;
Expand Down
73 changes: 36 additions & 37 deletions tests/integration/vercel-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,44 +204,43 @@ describe('Vercel SDK Instrumentation', () => {
it('should observe on a given thread', async () => {
const spy = jest.spyOn(client.api, 'sendSteps');

const thread = await client.thread({ name: 'VercelSDK Test' }).upsert();

const generateTextWithLiteralAI =
client.instrumentation.vercel.instrument(generateText);

const result = await generateTextWithLiteralAI({
model: openai('gpt-3.5-turbo'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
literalAiParent: thread
await client.thread({ name: 'VercelSDK Test' }).wrap(async () => {
const generateTextWithLiteralAI =
client.instrumentation.vercel.instrument(generateText);

const result = await generateTextWithLiteralAI({
model: openai('gpt-3.5-turbo'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.'
});

expect(result.text).toBeTruthy();

// Sending message is done asynchronously
await new Promise((resolve) => setTimeout(resolve, 10));

expect(spy).toHaveBeenCalledWith([
expect.objectContaining({
type: 'llm',
name: 'gpt-3.5-turbo',
threadId: client._currentThread()?.id,
generation: expect.any(Object),
input: {
content: [
{
role: 'user',
content: [
{
text: 'Write a vegetarian lasagna recipe for 4 people.',
type: 'text'
}
]
}
]
},
output: { role: 'assistant', content: result.text }
})
]);
});

expect(result.text).toBeTruthy();

// Sending message is done asynchronously
await new Promise((resolve) => setTimeout(resolve, 10));

expect(spy).toHaveBeenCalledWith([
expect.objectContaining({
type: 'llm',
name: 'gpt-3.5-turbo',
threadId: thread.id,
generation: expect.any(Object),
input: {
content: [
{
role: 'user',
content: [
{
text: 'Write a vegetarian lasagna recipe for 4 people.',
type: 'text'
}
]
}
]
},
output: { role: 'assistant', content: result.text }
})
]);
});

it('should monitor tools', async () => {
Expand Down
Loading