langfuse / langfuse

🪢 Open source LLM engineering platform: LLM Observability, metrics, evals, prompt management, playground, datasets. Integrates with LlamaIndex, Langchain, OpenAI SDK, LiteLLM, and more. 🍊YC W23
https://langfuse.com/docs
Other
5.62k stars 518 forks source link

feat: Langfuse observeOpenAI should log generations using new zodResponseFormat. #2862

Closed marcklingen closed 1 month ago

marcklingen commented 1 month ago

Discussed in https://github.com/orgs/langfuse/discussions/2858

Originally posted by **fabstu** August 7, 2024 OpenAI introduced [Structured Outputs](https://openai.com/index/introducing-structured-outputs-in-the-api/). It seems Langfuse does not log calls using it yet. Or am I doing something wrong? See the attached image showing the first completion appearing, but not the second. ```ts export async function testLangfuse() { const trace = langfuse.trace({ name: "testLangfuse" }); const span = trace.span({ name: "testLangfuseSpan" }); const llm = observeOpenAI( new OpenAI({ apiKey: process.env["OPENAI_API_KEY"], }), { parent: span, generationName: "testLangfuse", } ); const chatCompletion = await llm.chat.completions.create({ messages: [{ role: "system", content: "Say this is a test!" }], model: "gpt-3.5-turbo", user: "langfuse", max_tokens: 300, }); console.log("chatCompletion", chatCompletion); // Does not appear in span. zodResponseFormat requires gpt-4o-2024-08-06 or gpt-4o-mini. const GeneratedSchema = z.object({ output: z.string(), }); const chatCompletion2 = await llm.chat.completions.create({ messages: [{ role: "system", content: "Say this is a test!" }], model: "gpt-4o-2024-08-06", user: "langfuse", max_tokens: 300, response_format: zodResponseFormat(GeneratedSchema, "output"), }); console.log("chatCompletion2", chatCompletion2); console.log("TraceURL:", trace.getTraceUrl()); langfuse.flush(); } ``` Screenshot 2024-08-07 at 23 40 40
hassiebp commented 1 month ago

Fix released in https://github.com/langfuse/langfuse-js/releases/tag/v3.18.0 that parses response_format and adds it to the generation metadata