Skip to content
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

Feat/openai beta streaming #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
98 changes: 68 additions & 30 deletions examples/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,90 @@ import OpenAI from "openai"
import monitor from "../src/index"
import { monitorOpenAI } from "../src/openai"

monitor.init({
verbose: true,
})
monitor.init({ verbose: true })

// This extends the openai object with the monitor
const openai = monitorOpenAI(
new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
)

const assistant = await openai.beta.assistants.create({
model: "gpt-4-turbo",
instructions:
"You are a personal math tutor. When asked a math question, write and run code to answer the question.",
model: "gpt-4-1106-preview",
tools: [{ type: "code_interpreter" }],
})

console.log(assistant)

const thread = await openai.beta.threads.create({
messages: [
"You are a weather bot. Use the provided functions to answer questions.",
tools: [
{
role: "user",
content: "Hello!",
type: "function",
function: {
name: "getCurrentTemperature",
description: "Get the current temperature for a specific location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g., San Francisco, CA",
},
unit: {
type: "string",
enum: ["Celsius", "Fahrenheit"],
description:
"The temperature unit to use. Infer this from the user's location.",
},
},
required: ["location", "unit"],
},
},
},
{
role: "user",
content: "How can I help?",
type: "function",
function: {
name: "getRainProbability",
description: "Get the probability of rain for a specific location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g., San Francisco, CA",
},
},
required: ["location"],
},
},
},
],
})
]
});

const message = await openai.beta.threads.messages.create(thread.id, {
role: "user",
content: "What is 2 + 2?",
})
const thread = await openai.beta.threads.create()

async function assistantsAPI(input) {
await openai.beta.threads.messages.create(thread.id, {
role: "user", content: input
})

console.log(thread)
// const run = openai.beta.threads.runs
// .createAndStream(thread.id, {
// assistant_id: assistant.id,
// tags: ["demo", "test"]
// })
// // .on("event", (ev) => console.log(ev.event, ev.data))
// .on("end", async () => {
// await openai.beta.threads.del(thread.id);
// await openai.beta.assistants.del(assistant.id);
// })

const run = await openai.beta.threads.runs.create(thread.id, {
assistant_id: assistant.id,
})
const stream = await openai.beta.threads.runs.create(
thread.id, { assistant_id: assistant.id, stream: true }
);

console.log(run)
for await (const event of stream) {
console.log(event);
}
}

const messages = await openai.beta.threads.messages.list(thread.id)
await assistantsAPI("What's the weather in San Francisco today and the likelihood it'll rain?");

console.log(messages)
console.log("* assistant:", assistant.id);
console.log("* thread:", thread.id);
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading