Langfuse observation for openai embeddings method #3460
-
Hi,
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Hello @simrangulati-jktech! I'm here to help you with any bugs, questions, or contributions you have. Let's take a look at the issue you're facing with the OpenAI embeddings method using Langfuse. I found a similar discussion that might be relevant to your issue. In the discussion titled Generation does not come under @observe trace, it was mentioned that you need to get the callback handler from To trace the OpenAI embeddings method and see the cost or token count in the generated trace, you can follow these steps:
Here is an updated version of your code that includes these steps: import json
from langfuse.decorators import observe, langfuse_context
from langfuse import Langfuse
from openai import OpenAI
langfuse = Langfuse()
@observe()
def generate_openai_embeddings(text: str = "", model: str = "text-embedding-ada-002"):
if text == "":
return None
client = OpenAI(api_key=OPENAI_API_KEY)
text = text.replace("\n", " ")
# Update trace attributes if needed
langfuse_context.update_current_trace(
name="openai-embeddings-trace",
session_id="user-1234",
user_id="session-1234",
)
# Get the Langfuse handler for the current trace
langfuse_handler = langfuse_context.get_current_langchain_handler()
# Create embeddings with the Langfuse handler as a callback
embeddings = json.dumps(client.embeddings.create(input=[text], model=model, config={"callbacks": [langfuse_handler]}).data[0].embedding)
return embeddings
embeddings = generate_openai_embeddings(text="Open AI new Embeddings models is great.") This code ensures that the OpenAI embeddings method is properly traced, and the cost and token count should be visible in the Langfuse dashboard [1]. For more detailed information, you can refer to the Langfuse documentation [1]. |
Beta Was this translation helpful? Give feedback.
-
HI @simrangulati-jktech, can you try to trace the OpenAI embeddings method as an observation ( We currently have an open post in our GitHub Ideas board. Feel free to upvote here to get updates: https://github.com/orgs/langfuse/discussions/1021 |
Beta Was this translation helpful? Give feedback.
HI @simrangulati-jktech, can you try to trace the OpenAI embeddings method as an observation (
@observe(as_type="generation")
)? Langfuse tracing currently has 3 distinct observation types (docs): spans, events, generations. Currently embedding jobs can be tracked via generations (add all to input, cost tracking works as expected).We currently have an open post in our GitHub Ideas board. Feel free to upvote here to get updates: https://github.com/orgs/langfuse/discussions/1021