Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google-genai[minor]: Add support for json output #6519

Merged
merged 4 commits into from
Aug 16, 2024

Conversation

juji
Copy link
Contributor

@juji juji commented Aug 13, 2024

Support JSON output with google-genai, by adding a json: true to instance initialization.

Example:

const model = new ChatGoogleGenerativeAI({
  model: "gemini-1.5-flash",
  json: true
})

Usage Example:

const messages = [
  new SystemMessage(`
    Translate the following English text into Italian. 

    Reply with JSON, using the following JSON schema:
    {"translation": "string"}
  `),
  new HumanMessage("hi, i am superman"),
];

const model = new ChatGoogleGenerativeAI({
  model: "gemini-1.5-flash",
  json: true
})

const parser = new JsonOutputParser()
const result = await model.pipe(parser).invoke(messages);
console.log(result)

// {"translation": "Ciao, sono Superman"}

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 13, 2024
Copy link

vercel bot commented Aug 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Aug 16, 2024 9:48am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Aug 16, 2024 9:48am

@dosubot dosubot bot added the auto:improvement Medium size change to existing code to handle new use-cases label Aug 13, 2024
Copy link
Member

@bracesproul bracesproul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pushing these updates! It would be great if you could also push up a test verifying this mode works as intended. Thank you!

libs/langchain-google-genai/src/chat_models.ts Outdated Show resolved Hide resolved
libs/langchain-google-genai/src/chat_models.ts Outdated Show resolved Hide resolved
libs/langchain-google-genai/src/chat_models.ts Outdated Show resolved Hide resolved
libs/langchain-google-genai/src/chat_models.ts Outdated Show resolved Hide resolved
@bracesproul bracesproul changed the title support json output for google-genai google-genai[minor]: Add support for json output Aug 15, 2024
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Aug 16, 2024
@jacoblee93 jacoblee93 merged commit 9d05750 into langchain-ai:main Aug 16, 2024
26 checks passed
@jacoblee93
Copy link
Collaborator

Thank you!

CarterMorris pushed a commit to CarterMorris/langchainjs that referenced this pull request Nov 10, 2024
* support json output for google-genai

* Apply suggestions from code review

* Add test

---------

Co-authored-by: juji <him@jujiyangasli.com>
Co-authored-by: Brace Sproul <braceasproul@gmail.com>
Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:improvement Medium size change to existing code to handle new use-cases size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants