Unexpected error on self-host #5633
Answered
by
TristanBMuri
TristanBMuri
asked this question in
Support
-
Hi when I try to run the following code I get an unexpected error as a response. Below my console output. Do any of you know what might be the cause? from langfuse.decorators import observe, langfuse_context
from langchain_google_genai import ChatGoogleGenerativeAI
@observe(as_type="generation")
def gemini_completion(**kwargs):
"""
This function wraps a call to the Gemini model using ChatGoogleGenerativeAI.
It logs input parameters and (if available) usage details to Langfuse.
"""
# Clone kwargs and extract logging information.
kwargs_clone = kwargs.copy()
messages = kwargs_clone.pop('messages', None)
model = kwargs_clone.pop('model', None)
langfuse_context.update_current_observation(
input=messages,
model=model,
metadata=kwargs_clone
)
# Create a ChatGoogleGenerativeAI client with the desired parameters.
temperature = kwargs.get("temperature", 1)
max_tokens = kwargs.get("max_tokens", None)
timeout = kwargs.get("timeout", None)
max_retries = kwargs.get("max_retries", 2)
llm = ChatGoogleGenerativeAI(
model=model,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout,
max_retries=max_retries
)
# Invoke the model using the provided messages.
response = llm.invoke(messages)
# Update Langfuse observation with usage details if available.
try:
langfuse_context.update_current_observation(
usage_details={
"input": response.usage.input_tokens,
"output": response.usage.output_tokens
}
)
except Exception:
pass # Skip if usage details are not available.
# Return the generated text.
return response.content if hasattr(response, "content") else response
@observe()
def main():
"""
Example main function that calls gemini_completion with the Gemini model.
"""
return gemini_completion(
model="gemini-2.0-flash-exp",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Gemini!"}
]
)
if __name__ == "__main__":
result = main()
print("Gemini API response:\n", result) |
Beta Was this translation helpful? Give feedback.
Answered by
TristanBMuri
Feb 19, 2025
Replies: 1 comment 5 replies
-
Did this work for you before? How did you set up the Langfuse credentials? |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay my problem has an easy fix:
Step 1 of 1:
Set your host to:
LANGFUSE_HOST=http://localhost:3000
instead of:
LANGFUSE_HOST="http://localhost:3000"