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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for cohere models #585
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
馃憤 Looks good to me!
- Reviewed the entire pull request up to ba6c246
- Looked at
457
lines of code in11
files - Took 2 minutes and 43 seconds to review
More info
- Skipped
1
files when reviewing. - Skipped posting
9
additional comments because they didn't meet confidence threshold of85%
.
1. instructor/client_cohere.py:160
:
- Assessed confidence :
0%
- Comment:
The new fileclient_cohere.py
is added to support cohere models. The functionfrom_cohere
is defined to create an instructor for cohere models. The functioncreate_cohere_structured_output
is defined to generate a tool snippet for a pydantic object, format a tool use prompt with the tool snippet, and send the tool use prompt to cohere client chat. The functionprompt_model_for_python_class_def
is defined to generate a prompt to generate python code that defines the provided pydantic object. The functiongenerate_tool_snippet_pydantic
is defined to generate a string representing Python code to define a tool which takes pydantic object as a parameter. The functionrender_chat_history
is defined to render chat history as a string. The functionformat_tool_use_prompt
is defined to format tool use prompt with code for tools being provided bytool_snippet
. The code seems to be logically correct and follows the best practices. - Reasoning:
The new fileclient_cohere.py
is added to support cohere models. The functionfrom_cohere
is defined to create an instructor for cohere models. The functioncreate_cohere_structured_output
is defined to generate a tool snippet for a pydantic object, format a tool use prompt with the tool snippet, and send the tool use prompt to cohere client chat. The functionprompt_model_for_python_class_def
is defined to generate a prompt to generate python code that defines the provided pydantic object. The functiongenerate_tool_snippet_pydantic
is defined to generate a string representing Python code to define a tool which takes pydantic object as a parameter. The functionrender_chat_history
is defined to render chat history as a string. The functionformat_tool_use_prompt
is defined to format tool use prompt with code for tools being provided bytool_snippet
. The code seems to be logically correct and follows the best practices.
2. instructor/function_calls.py:160
:
- Assessed confidence :
0%
- Comment:
The functionparse_cohere_tools
is added to parse the response from cohere tools. The functionparse_cohere_tools
extracts the json text from the response, loads it into a dictionary, extracts the parameters from the dictionary, and validates the parameters using the pydantic model. The code seems to be logically correct and follows the best practices. - Reasoning:
The functionparse_cohere_tools
is added to parse the response from cohere tools. The functionparse_cohere_tools
extracts the json text from the response, loads it into a dictionary, extracts the parameters from the dictionary, and validates the parameters using the pydantic model. The code seems to be logically correct and follows the best practices.
3. instructor/mode.py:17
:
- Assessed confidence :
0%
- Comment:
The modeCOHERE_TOOLS
is added to theMode
enum. This mode is used to indicate that the cohere tools are being used. The code seems to be logically correct and follows the best practices. - Reasoning:
The modeCOHERE_TOOLS
is added to theMode
enum. This mode is used to indicate that the cohere tools are being used. The code seems to be logically correct and follows the best practices.
4. instructor/process_response.py:329
:
- Assessed confidence :
0%
- Comment:
The functionhandle_response_model
is updated to handle the case when the mode isCOHERE_TOOLS
. In this case, theresponse_model
is added to thekwargs
. The code seems to be logically correct and follows the best practices. - Reasoning:
The functionhandle_response_model
is updated to handle the case when the mode isCOHERE_TOOLS
. In this case, theresponse_model
is added to thekwargs
. The code seems to be logically correct and follows the best practices.
5. instructor/retry.py:48
:
- Assessed confidence :
0%
- Comment:
The functionreask_messages
is updated to handle the case when the mode isCOHERE_TOOLS
. In this case, a user message is yielded with the validation error and a request to recall the function correctly and fix the errors. The code seems to be logically correct and follows the best practices. - Reasoning:
The functionreask_messages
is updated to handle the case when the mode isCOHERE_TOOLS
. In this case, a user message is yielded with the validation error and a request to recall the function correctly and fix the errors. The code seems to be logically correct and follows the best practices.
6. instructor/utils.py:50
:
- Assessed confidence :
0%
- Comment:
The functionextract_python_from_codeblock
is added to extract python code from a code block in a string. The function finds the start and end of the python code block and returns the python code. The code seems to be logically correct and follows the best practices. - Reasoning:
The functionextract_python_from_codeblock
is added to extract python code from a code block in a string. The function finds the start and end of the python code block and returns the python code. The code seems to be logically correct and follows the best practices.
7. pyproject.toml:33
:
- Assessed confidence :
0%
- Comment:
Thecohere
package is added as an optional dependency. This package is required to use the cohere models. The code seems to be logically correct and follows the best practices. - Reasoning:
Thecohere
package is added as an optional dependency. This package is required to use the cohere models. The code seems to be logically correct and follows the best practices.
8. requirements.txt:10
:
- Assessed confidence :
0%
- Comment:
Thecohere
package is added to the requirements file. This package is required to use the cohere models. The code seems to be logically correct and follows the best practices. - Reasoning:
Thecohere
package is added to the requirements file. This package is required to use the cohere models. The code seems to be logically correct and follows the best practices.
9. tests/test_new_client.py:232
:
- Assessed confidence :
0%
- Comment:
The testtest_client_cohere_response
is added to test the response from the cohere client. The testtest_client_cohere_response_with_nested_classes
is added to test the response from the cohere client with nested classes. The tests seem to be logically correct and follow the best practices. - Reasoning:
The testtest_client_cohere_response
is added to test the response from the cohere client. The testtest_client_cohere_response_with_nested_classes
is added to test the response from the cohere client with nested classes. The tests seem to be logically correct and follow the best practices.
Workflow ID: wflow_pLNZL6k6ZoxnkEmk
Not what you expected? You can customize the content of the reviews using rules. Learn more here.
instructor/client_cohere.py
Outdated
(2) formats a tool use prompt with the tool snippet | ||
(3) sends the tool use prompt to cohere client chat | ||
""" | ||
if "messages" in kwargs and "response_model" in kwargs: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh my god this is brutal
instructor/client_cohere.py
Outdated
The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral. | ||
|
||
# System Preamble | ||
## Basic Rules |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this their system message???
Do you think we're better off just asking for a json? |
Ok asking for json directly removes a lot of the gnarliness 馃槄 |
|
||
@overload | ||
def from_cohere( | ||
client: cohere.Client, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there an async client we need to watch out for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yes there's cohere.AsyncClient
for some reason instructor.utils.is_async
is not recognizing its client.chat
function as async and so patching doesn't work...will send a follow up a bit later today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should support both now!
do you mind just update the docs or adding a generic |
Ok added a few basic examples (like with |
@@ -0,0 +1,143 @@ | |||
# Document Segmentation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you'll have to add this in the mkdocs.yml file
or even reference it in examples/index.md
Trying to add support for cohere models (
command-r
andcommand-r-plus
) leveraging their function calling ability.Looking at the tool-use prompt template from the docs here, the tools are defined as python functions and so I had the following idea:
.model_json_schema()
max_retries
is reachedThis leads to less retries than asking the model to output json based on
.model_json_schema()
directly, but is more costly for simple use cases (non-nested pydantic objects) where the extra llm call is not really necessary.Summary:
This PR adds support for Cohere models in the Instructor library, introducing a new mode and function to handle Cohere's function calling ability, along with necessary updates in various files, tests, and dependencies.
Key points:
instructor
library.COHERE_TOOLS
, ininstructor/mode.py
.from_cohere
, ininstructor/client_cohere.py
to handle Cohere's function calling ability.instructor/__init__.py
to importfrom_cohere
if Cohere is available.instructor/function_calls.py
to handleCOHERE_TOOLS
mode.instructor/process_response.py
to handleCOHERE_TOOLS
mode.instructor/retry.py
to handleCOHERE_TOOLS
mode.instructor/utils.py
to includeCOHERE
inProvider
enum.pyproject.toml
andrequirements.txt
to include Cohere as a dependency.tests/test_new_client.py
.Generated with 鉂わ笍 by ellipsis.dev