feat(genie): add TogetherGenie for TogetherAI integration#112
feat(genie): add TogetherGenie for TogetherAI integration#112KacemMathlouthi wants to merge 6 commits intochonkie-inc:mainfrom
Conversation
There was a problem hiding this comment.
Hello @KacemMathlouthi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request introduces a new TogetherGenie class to enable integration with TogetherAI models within the Chonkie framework. It supports both standard text generation and structured JSON output using Pydantic schemas, leveraging TogetherAI's capabilities. The PR also includes the addition of together and pydantic as dependencies in the pyproject.toml file.
Highlights
- New Feature: TogetherAI Integration: Adds
TogetherGenieclass for interacting with TogetherAI models. - JSON Schema Support: Implements
generate_jsonmethod for structured JSON output using Pydantic schemas, validating against a list of supported models. - Dependency Management: Adds
togetherandpydanticas dependencies topyproject.toml.
Changelog
Click here to see the changelog
- pyproject.toml
- Added
pydantic[dev]>=2.11.2as a dependency at line 50. - Added
together[dev]>=1.4.6as a dependency at line 51.
- Added
- src/chonkie/genie/init.py
- Imported
TogetherGeniefromchonkie.genie.togetherat line 6. - Added
TogetherGenieto the__all__list at line 13.
- Imported
- src/chonkie/genie/together.py
- Created new file
src/chonkie/genie/together.pyimplementing theTogetherGenieclass. - Implements
generatefor basic chat completions. - Implements
generate_jsonfor structured JSON output. - Includes a list of supported models for JSON mode.
- Adds dependency management for
togetherandpydantic.
- Created new file
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
A Genie new,
TogetherAI's call,
JSON takes its form.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This PR introduces a new TogetherGenie class to integrate TogetherAI models into Chonkie, supporting both text generation and structured JSON outputs. The implementation looks good overall, with clear separation of concerns and adherence to best practices. However, there are a few areas that could be improved for better maintainability and clarity.
Summary of Findings
- Dependency Management: Consider grouping dependencies in
pyproject.tomlby purpose rather than alphabetically for better readability and maintainability. - Global Imports: The
_import_dependenciesmethod usesglobalto import dependencies, which can make the code harder to understand and maintain. Consider refactoring this to avoid usingglobal. - Error Message Clarity: The error message in the
ImportErrorcould be more user-friendly by providing specific instructions on how to install the dependencies.
Merge Readiness
The code is well-structured and implements the desired functionality. However, addressing the comments regarding dependency management and global imports would improve the code's maintainability and readability. I recommend addressing these points before merging. I am unable to directly approve this pull request, and other reviewers should review and approve this code before merging.
src/chonkie/genie/together.py
Outdated
| if TYPE_CHECKING: | ||
| try: | ||
| from together import Together | ||
| except ImportError: | ||
| Together = Any | ||
| try: | ||
| from pydantic import BaseModel | ||
| except ImportError: | ||
| BaseModel = Any |
There was a problem hiding this comment.
Consider using typing.TYPE_CHECKING to avoid circular dependencies and improve type checking performance. You can move the import statements inside the if TYPE_CHECKING: block to prevent runtime import errors when these dependencies are not installed. This is especially useful for optional dependencies like together and pydantic.
if TYPE_CHECKING:
from together import Together
from pydantic import BaseModel
src/chonkie/genie/together.py
Outdated
| api_key (Optional[str]): The API key to use. Defaults to the environment variable TOGETHER_API_KEY. | ||
| """ | ||
| super().__init__() | ||
| self._import_dependencies() |
src/chonkie/genie/together.py
Outdated
| if self._is_available(): | ||
| global Together, BaseModel | ||
| from together import Together | ||
| from pydantic import BaseModel | ||
| else: | ||
| raise ImportError( | ||
| "TogetherGenie requires `together` and `pydantic`. Install with `pip install together pydantic`" | ||
| ) |
There was a problem hiding this comment.
The _import_dependencies method uses global to import Together and BaseModel. While this works, it can make the code harder to understand and maintain. A better approach would be to assign these dependencies to class attributes or use them directly within the methods where they are needed. Consider refactoring this to avoid using global.
from together import Together
from pydantic import BaseModelThere was a problem hiding this comment.
Pull request overview
This PR adds support for TogetherAI model integration by introducing a new TogetherGenie class, enabling both standard text generation and structured JSON outputs with pydantic schema validation.
Changes:
- Added
TogetherGenieclass with support for text generation and JSON-mode responses - Defined a list of models that support JSON mode based on TogetherAI documentation
- Integrated the new class into the genie module exports
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/chonkie/genie/together.py | New file implementing TogetherGenie with generate() and generate_json() methods |
| src/chonkie/genie/init.py | Added TogetherGenie to module imports and exports |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/chonkie/genie/together.py
Outdated
| import importlib.util as importutil | ||
| import os | ||
| from typing import TYPE_CHECKING, Any, Dict, Optional | ||
| import json |
There was a problem hiding this comment.
The json module is imported but never used in this file. Consider removing this unused import to keep the code clean.
| import json |
feat(genie): Add TogetherGenie for TogetherAI model integration
Summary
This PR introduces a new
TogetherGenieclass in thegeniemodule, allowing Chonkie to use TogetherAI models. It supports standard text generation and structured JSON outputs usingpydanticschemas, following TogetherAI's documented capabilities.Features
TogetherGenie.generate(prompt: str)— sends a prompt to a TogetherAI model and returns plain text output.TogetherGenie.generate_json(prompt: str, schema: BaseModel)— uses TogetherAI's [structured output support](https://docs.together.ai/docs/json-mode) to return a dictionary validated against apydanticschema.Supported Models for JSON Mode
A set of supported models is defined, based on: https://docs.together.ai/docs/json-mode
The full list is maintained in the
SUPPORTED_JSON_MODELSconstant.Dependency Notes:
The following dependencies were added under the
devgroup inpyproject.toml:togetherpydantic