Created
April 9, 2025 21:15
-
-
Save perryism/ace246533ab439bb6ebc018a3a6d3345 to your computer and use it in GitHub Desktop.
Gemini with structured output using langchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from langchain_google_genai import ChatGoogleGenerativeAI | |
import os | |
llm = ChatGoogleGenerativeAI( | |
model="gemini-2.0-flash-001", | |
temperature=0, | |
max_tokens=None, | |
timeout=None, | |
max_retries=2, | |
) | |
from pydantic import BaseModel | |
class Sentence(BaseModel): | |
original: str | |
translated: str | |
messages = [ | |
( | |
"system", | |
"You are a helpful assistant that translates English to French. Translate the user sentence.", | |
), | |
("human", "I love programming."), | |
] | |
structured_llm = llm.with_structured_output(Sentence) | |
ai_msg = structured_llm.invoke(messages) | |
ai_msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment