You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case someone finds it useful.
If It can be improved, even better.
This is just one way to do it.
You can test this very quickly on https://colab.research.google.com/
Test integration of langfuse + instructor with openai / gemini using openai's python SDK
LANGFUSE_SECRET_KEY="sk-..."LANGFUSE_PUBLIC_KEY="pk-..."LANGFUSE_HOST="https://cloud.langfuse.com"# different if in USOPENAI_API_KEY='sk-...'GEMINI_API_KEY='...'
# Install
!pipinstalllangfuseopenaiinstructor# Import required modules# from openai import OpenAI # Not anymoreimportlangfuseimportinstructor
# This wraps OpenAI to include Langfuse trackingfromlangfuse.openaiimportopenai# Configure Langfuse credentialsopenai.langfuse_public_key=LANGFUSE_PUBLIC_KEYopenai.langfuse_secret_key=LANGFUSE_SECRET_KEYopenai.langfuse_host=LANGFUSE_HOST
# Validate Langfuse authentication (not needed, just for testing)openai.langfuse_auth_check()
True
# now, patch the clients with instructor#!pip install instructorimportinstructor
# Create clients with Langfuse integration patched by Instructorclient_openai_with_langfuse=instructor.patch(
openai.OpenAI(api_key=OPENAI_API_KEY)
)
# Gemini can use the same python SDK, just need different KEY and HOSTclient_gemini_with_langfuse=instructor.patch(
openai.OpenAI(api_key=GEMINI_API_KEY,
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
)
# A simple example of call to llm using instructor's response_modelfrompydanticimportBaseModelclassOutput(BaseModel):
name: strage: intprompt="John has 3 children, 1 car and is 33 years old."# send callresponse=client_openai_with_langfuse.chat.completions.create(
model=model_openai,
messages=[{"role": "user", "content": prompt}],
response_model=Output,
tags=["test","openai"], # optional: for langfuse only
)
print(response)
response=client_gemini_with_langfuse.chat.completions.create(
model=model_gemini,
messages=[{"role": "user", "content": prompt}],
response_model=Output,
tags=["test","gemini"], # optional: for langfuse only
)
print(response)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
instructor https://github.com/instructor-ai/instructor
langfuse https://cloud.langfuse.com/
Test integration of langfuse + instructor with openai / gemini using openai's python SDK
Beta Was this translation helpful? Give feedback.
All reactions