-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenai.test.ts
104 lines (87 loc) · 2.13 KB
/
openai.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {
expect,
test,
describe,
beforeEach,
afterEach,
afterAll,
spyOn,
} from "bun:test"
import lunary from "../src/index"
import { monitorOpenAI } from "../src/openai"
import OpenAI from "openai"
lunary.init({
verbose: true,
})
const expectLogsContains = (spy, str: string) => {
expect(spy).toHaveBeenCalledWith(expect.stringContaining(str))
}
describe("openai", () => {
let spy
beforeEach(() => {
spy = spyOn(console, "log").mockImplementation((d) => {
process.stdout.write(d + "\n")
})
})
afterEach(() => {
spy.mockClear()
})
afterAll(() => {
spy.mockRestore()
})
test("Basic OpenAI", async () => {
const openai = monitorOpenAI(
new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
)
const result = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
temperature: 0,
tags: ["translate"],
user: "user123",
seed: 123,
userProps: {
name: "John Doe",
},
tools: [
{
type: "function",
function: {
name: "translate",
parameters: {
type: "object",
properties: {
text: { type: "string" },
from: { type: "string" },
to: { type: "string" },
},
},
},
},
],
messages: [
{
role: "user",
content: `Hello, translate 'Bonjour' from french to english`,
},
],
})
expect(result.model).toContain("gpt-3.5-turbo")
// @ts-ignore
// Make sure the response structure is correct
expect(result.choices[0].message?.tool_calls[0]?.function.name).toBe(
"translate"
)
expectLogsContains(spy, `\"event\": \"start\"`)
expectLogsContains(spy, `\"name\": \"John Doe\"`)
expectLogsContains(spy, `\"event\": \"end\"`)
expectLogsContains(
spy,
`\"content\": \"Hello, translate 'Bonjour' from french to english\"`
)
// Make sure it reports the tool calls
expectLogsContains(spy, `\"arguments\": \"{`)
expectLogsContains(spy, `Events sent`)
})
})