@@ -94,48 +94,40 @@ chat_completion = patched_client.chat.completions.create(
94
94
)
95
95
` ),
96
96
TypeScriptBlock (` import OpenAI from "openai";
97
- import { traceable } from "langsmith/traceable";
97
+ import { traceable, getCurrentRunTree } from "langsmith/traceable";
98
98
import { wrapOpenAI } from "langsmith/wrappers";
99
- import { RunTree} from "langsmith";\n
100
- const client = new OpenAI();\n
101
- const messages = [
102
- {role: "system", content: "You are a helpful assistant."},
103
- {role: "user", content: "Hello!"}
104
- ];\n
105
- const traceableCallOpenAI = traceable(async (messages: {role: string, content: string}[]) => {
106
- const completion = await client.chat.completions.create({
107
- model: "gpt-4o-mini",
108
- messages: messages,
109
- });
110
- return completion.choices[0].message.content;
111
- },{
112
- run_type: "llm",
113
- name: "OpenAI Call Traceable",
114
- // highlight-next-line
115
- tags: ["my-tag"],
116
- // highlight-next-line
117
- metadata: { "my-key": "my-value" }
99
+
100
+ const client = wrapOpenAI(new OpenAI());
101
+
102
+ const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
103
+ { role: "system", content: "You are a helpful assistant." },
104
+ { role: "user", content: "Hello!" },
105
+ ];
106
+
107
+ const traceableCallOpenAI = traceable(
108
+ async (messages: OpenAI.Chat.ChatCompletionMessageParam[]) => {
109
+ const completion = await client.chat.completions.create({
110
+ model: "gpt-4o-mini",
111
+ messages,
118
112
});
113
+ const runTree = getCurrentRunTree();
114
+ runTree.extra.metadata = {
115
+ ...runTree.extra.metadata,
116
+ someKey: "someValue",
117
+ };
118
+ runTree.tags = [...(runTree.tags ?? []), "runtime-tag"];
119
+ return completion.choices[0].message.content;
120
+ },
121
+ {
122
+ run_type: "llm",
123
+ name: "OpenAI Call Traceable",
124
+ tags: ["my-tag"],
125
+ metadata: { "my-key": "my-value" },
126
+ }
127
+ );
119
128
// Call the traceable function
120
- await traceableCallOpenAI(messages, "gpt-4o-mini");\n
121
- // Create a RunTree object
122
- const rt = new RunTree({
123
- run_type: "llm",
124
- name: "OpenAI Call RunTree",
125
- inputs: { messages },
126
- // highlight-next-line
127
- tags: ["my-tag"],
128
- // highlight-next-line
129
- extra: { metadata: { "my-key": "my-value" } },
130
- });
131
- await rt.postRun();
132
- const chatCompletion = await client.chat.completions.create({
133
- model: "gpt-4o-mini",
134
- messages: messages,
135
- });
136
- // End and submit the run
137
- await rt.end(chatCompletion);
138
- await rt.patchRun(); ` ),
139
- ]}
140
- groupId = " client-language"
129
+ await traceableCallOpenAI(messages);
130
+ ` ),
131
+ ]}
132
+ groupId = " client-language"
141
133
/>
0 commit comments