Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcadams committed Nov 8, 2023
1 parent 81fc8d2 commit 834b0e2
Show file tree
Hide file tree
Showing 4 changed files with 668 additions and 546 deletions.
42 changes: 21 additions & 21 deletions examples/next-13/src/app/code-chat.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Suspense } from "react";
import { Suspense } from 'react';

import hop from "hopfield";
import openai from "hopfield/openai";
import OpenAI from "openai";
import { kv } from "@vercel/kv";
import { docs } from "./docs";
import { hashString } from "./hash";
import hop from 'hopfield';
import openai from 'hopfield/openai';
import OpenAI from 'openai';
import { kv } from '@vercel/kv';
import { docs } from './docs';
import { hashString } from './hash';

// Create an OpenAI API client
const openaiClient = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || "",
apiKey: process.env.OPENAI_API_KEY || '',
});

// Instantiate a new Hopfield client with the OpenAI API client
const hopfield = hop.client(openai).provider(openaiClient);

// Create the Hopfield streaming chat provider
const chat = hopfield.chat("gpt-3.5-turbo-1106").streaming();
const chat = hopfield.chat('gpt-3.5-turbo-1106').streaming();

const prompt = `Provide a cool use of Hopfield from the context below, with a short paragraph introduction of what Hopfield does, and then a Typescript example in 20 lines of code or less: \n\n${docs}`;

Expand All @@ -32,12 +32,12 @@ export async function CodeChat() {
// construct messages with hop.inferMessageInput
const messages: hop.inferMessageInput<typeof chat>[] = [
{
role: "system",
role: 'system',
content:
"You are a developer evangelist for the Hopfield Typescript npm package. You ALWAYS respond using Markdown. The docs for Hopfield are located at https://hopfield.ai.",
'You are a developer evangelist for the Hopfield Typescript npm package. You ALWAYS respond using Markdown. The docs for Hopfield are located at https://hopfield.ai.',
},
{
role: "user",
role: 'user',
content: prompt,
},
];
Expand All @@ -52,11 +52,11 @@ export async function CodeChat() {
// we map to a string to store in Redis, to save on costs :sweat:
const storedResponse = chunks
.map((chunk) =>
chunk.choices[0].__type === "content"
chunk.choices[0].__type === 'content'
? chunk.choices[0].delta.content
: ""
: '',
)
.join("");
.join('');

await kv.set(promptHash, storedResponse);
// expire every ten minutes
Expand Down Expand Up @@ -105,7 +105,7 @@ async function RecursiveTokens({ reader }: RecursiveTokensProps) {

return (
<>
{value.choices[0].__type === "content" ? (
{value.choices[0].__type === 'content' ? (
value.choices[0].delta.content
) : (
<></>
Expand All @@ -125,18 +125,18 @@ const getCachedResponse = async (prompt: string) => {
const cached = (await kv.get(prompt)) as string | undefined;

if (cached) {
const chunks = cached.split(" ");
const chunks = cached.split(' ');
const stream = new ReadableStream<hop.inferResult<typeof chat>>({
async start(controller) {
let id = 0;
for (const chunk of chunks) {
const fakeChunk: hop.inferResult<typeof chat> = {
model: "gpt-3.5-turbo-1106",
model: 'gpt-3.5-turbo-1106',
id: String(id++),
created: Date.now(),
choices: [
{
__type: "content",
__type: 'content',
delta: {
content: `${chunk} `,
},
Expand All @@ -150,8 +150,8 @@ const getCachedResponse = async (prompt: string) => {
setTimeout(
r,
// get a random number between 10ms and 50ms to simulate a random delay
Math.floor(Math.random() * 40) + 10
)
Math.floor(Math.random() * 40) + 10,
),
);
}
controller.close();
Expand Down
21 changes: 10 additions & 11 deletions src/openai/chat/non-streaming-with-functions.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TEST_TIMEOUT = 8_000;
const hopfield = hop.client(openai).provider(openAIClient);

// change the model here to see performance diff
const hopfieldChat = hopfield.chat('gpt-3.5-turbo-1106');
const hopfieldChat = hopfield.chat('gpt-4-0613');

const classifyMessage = hopfield.function({
name: 'classifyMessage',
Expand All @@ -35,8 +35,8 @@ const getMessageActions = hopfield.function({
'A short summary of the message. This MUST be less than 5 words.',
),
links: z
.array(z.string().describe('Markdown link.'))
.describe('ALL Markdown links in the message.'),
.array(z.string().describe('Link parsed from Markdown.'))
.describe('All links in the message.'),
}),
});

Expand Down Expand Up @@ -83,17 +83,17 @@ describe.concurrent('non-streaming with functions', () => {
'https://githubbox.com/pmndrs/zustand/tree/main/examples/demo',
] as const;

expect(
parsed.choices[0].__type === 'function_call' &&
parsed.choices[0].message.function_call.arguments.links.length,
).toEqual(actualLinks.length);

for (const link of actualLinks) {
expect(
parsed.choices[0].__type === 'function_call' &&
parsed.choices[0].message.function_call.arguments.links,
).toContain(link);
}

expect(
parsed.choices[0].__type === 'function_call' &&
parsed.choices[0].message.function_call.arguments.links.length,
).toEqual(actualLinks.length);
},
TEST_TIMEOUT * 2,
);
Expand Down Expand Up @@ -132,7 +132,6 @@ describe.concurrent('non-streaming with functions', () => {
'https://johns-hopkins.com/3322301-sflk-1jsdlkfj/2904nd',
'https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/',
'https://github.com/sponsors/colinhacks',
'https://github.com/sponsors/colinhacks',
];

expect(
Expand Down Expand Up @@ -180,8 +179,8 @@ describe.concurrent('non-streaming with functions', () => {

expect(
parsed.choices[0].__type === 'function_call' &&
parsed.choices[0].message.function_call.arguments.links,
).toContain(actualLink);
parsed.choices[0].message.function_call.arguments.links?.[0],
).toEqual(actualLink);

expect(
parsed.choices[0].__type === 'function_call' &&
Expand Down

1 comment on commit 834b0e2

@vercel
Copy link

@vercel vercel bot commented on 834b0e2 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.