-
Notifications
You must be signed in to change notification settings - Fork 822
Add Cohere as an Inference Provider #2888
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3756a45
Add Cohere as an Inference Provider
alexrs-cohere 5dfa11d
Use new Cohere OpenAI compatible API
alexrs-cohere adb4405
Update docs/source/en/guides/inference.md
Wauplin f6ab0e9
Merge branch 'main' into ars-cohere-provider
Wauplin 2b5dc75
Update src/huggingface_hub/inference/_providers/cohere.py
alexrs-cohere a657e22
Merge branch 'main' into ars-cohere-provider
Wauplin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from huggingface_hub.inference._providers._common import ( | ||
BaseConversationalTask, | ||
) | ||
|
||
|
||
_PROVIDER = "cohere" | ||
_BASE_URL = "https://api.cohere.com" | ||
|
||
|
||
class CohereConversationalTask(BaseConversationalTask): | ||
def __init__(self): | ||
super().__init__(provider=_PROVIDER, base_url=_BASE_URL) | ||
|
||
def _prepare_route(self, mapped_model: str) -> str: | ||
if self.task == "conversational": | ||
return "/compatibility/v1/chat/completions" | ||
raise ValueError(f"Unsupported task '{self.task}' for Cohere API.") | ||
102 changes: 102 additions & 0 deletions
102
.../cassettes/TestInferenceClient.test_chat_completion_no_stream[cohere,conversational].yaml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
interactions: | ||
- request: | ||
body: '{"messages": [{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "What is deep learning?"}], "model": "command-r7b-12-2024", | ||
"stream": false}' | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '181' | ||
Content-Type: | ||
- application/json | ||
X-Amzn-Trace-Id: | ||
- 204391c6-92c8-4214-a394-04b025f3e86a | ||
method: POST | ||
uri: https://api.cohere.com/compatibility/v1/chat/completions | ||
response: | ||
body: | ||
string: '{"id":"3b5751bb-10a2-4fc8-95a0-d1e6cfa788b3","choices":[{"index":0,"finish_reason":"stop","message":{"role":"assistant","content":"Deep | ||
learning is a subfield of machine learning and artificial intelligence that | ||
focuses on training artificial neural networks to learn and make predictions | ||
from data. It is inspired by the structure and function of the human brain, | ||
particularly the interconnected network of neurons.\n\nIn deep learning, artificial | ||
neural networks are composed of multiple layers of interconnected nodes, or | ||
\"neurons,\" which process and transform input data. These networks are designed | ||
to automatically learn and extract hierarchical representations of data through | ||
a process called \"training.\" The training process involves adjusting the | ||
network''s internal parameters (weights and biases) to minimize the difference | ||
between predicted and actual outputs.\n\nHere are some key characteristics | ||
and concepts in deep learning:\n\n1. Neural Networks: Deep learning models | ||
are primarily based on artificial neural networks, which are composed of layers | ||
of nodes. These networks can have various architectures, such as convolutional | ||
neural networks (CNNs) for image processing, recurrent neural networks (RNNs) | ||
for sequential data, and transformer networks for natural language processing.\n\n2. | ||
Deep Architecture: The term \"deep\" in deep learning refers to the depth | ||
of the neural network, meaning it has multiple hidden layers between the input | ||
and output layers. These hidden layers enable the network to learn complex | ||
patterns and representations from the data.\n\n3. Learning and Training: Deep | ||
learning models are trained using large amounts of labeled data and a process | ||
called backpropagation. During training, the network adjusts its internal | ||
parameters to minimize a loss function, which measures the difference between | ||
predicted and actual outputs. This optimization process is typically done | ||
using gradient descent or its variants.\n\n4. Feature Learning: One of the | ||
key advantages of deep learning is its ability to automatically learn relevant | ||
features from raw data. Unlike traditional machine learning, where feature | ||
engineering is required, deep learning models can discover and extract features | ||
at multiple levels of abstraction.\n\n5. Applications: Deep learning has been | ||
applied to a wide range of tasks and domains, including image and speech recognition, | ||
natural language processing, object detection, medical diagnosis, game playing | ||
(e.g., AlphaGo), and autonomous driving.\n\nDeep learning has revolutionized | ||
many areas of artificial intelligence due to its ability to handle complex | ||
and large-scale data, learn hierarchical representations, and achieve state-of-the-art | ||
performance in various tasks. It has driven significant advancements in areas | ||
like computer vision, natural language understanding, and speech recognition."}}],"created":1740653732,"model":"command-r7b-12-2024","object":"chat.completion","usage":{"prompt_tokens":11,"completion_tokens":476,"total_tokens":487}}' | ||
headers: | ||
Alt-Svc: | ||
- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 | ||
Transfer-Encoding: | ||
- chunked | ||
Via: | ||
- 1.1 google | ||
access-control-expose-headers: | ||
- X-Debug-Trace-ID | ||
cache-control: | ||
- no-cache, no-store, no-transform, must-revalidate, private, max-age=0 | ||
content-type: | ||
- application/json | ||
date: | ||
- Thu, 27 Feb 2025 10:55:32 GMT | ||
expires: | ||
- Thu, 01 Jan 1970 00:00:00 UTC | ||
num_chars: | ||
- '2831' | ||
num_tokens: | ||
- '487' | ||
pragma: | ||
- no-cache | ||
server: | ||
- envoy | ||
vary: | ||
- Origin | ||
x-accel-expires: | ||
- '0' | ||
x-api-warning: | ||
- Please set an API version, for more information please refer to https://docs.cohere.com/versioning-reference | ||
- Version is deprecated, for more information please refer to https://docs.cohere.com/versioning-reference | ||
x-debug-trace-id: | ||
- 430c1e5519b95b094771bcc36304445e | ||
x-envoy-upstream-service-time: | ||
- '2740' | ||
x-trial-endpoint-call-limit: | ||
- '100' | ||
x-trial-endpoint-call-remaining: | ||
- '99' | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
146 changes: 146 additions & 0 deletions
146
...assettes/TestInferenceClient.test_chat_completion_with_stream[cohere,conversational].yaml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
interactions: | ||
- request: | ||
body: '{"messages": [{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "What is deep learning?"}], "model": "command-r7b-12-2024", | ||
"max_tokens": 20, "stream": true}' | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '198' | ||
Content-Type: | ||
- application/json | ||
X-Amzn-Trace-Id: | ||
- 68c492d9-abbd-4d0a-8462-e598765021e4 | ||
method: POST | ||
uri: https://api.cohere.com/compatibility/v1/chat/completions | ||
response: | ||
body: | ||
string: 'data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":"","role":"assistant"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":"Deep"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
learning"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
is"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
a"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
sub"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":"field"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
of"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
machine"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
learning"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
and"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
artificial"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
intelligence"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
that"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
focuses"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
on"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
training"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
artificial"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
neural"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":null,"delta":{"content":" | ||
networks"}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk"} | ||
|
||
|
||
data: {"id":"2bb1b33e-53d9-4fae-8958-2e54c1e60f09","choices":[{"index":0,"finish_reason":"length","delta":{}}],"created":1740653733,"model":"command-r7b-12-2024","object":"chat.completion.chunk","usage":{"prompt_tokens":11,"completion_tokens":19,"total_tokens":30}} | ||
|
||
|
||
data: [DONE] | ||
|
||
|
||
' | ||
headers: | ||
Alt-Svc: | ||
- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 | ||
Transfer-Encoding: | ||
- chunked | ||
Via: | ||
- 1.1 google | ||
access-control-expose-headers: | ||
- X-Debug-Trace-ID | ||
cache-control: | ||
- no-cache, no-store, no-transform, must-revalidate, private, max-age=0 | ||
content-type: | ||
- text/event-stream | ||
date: | ||
- Thu, 27 Feb 2025 10:55:33 GMT | ||
expires: | ||
- Thu, 01 Jan 1970 00:00:00 UTC | ||
pragma: | ||
- no-cache | ||
server: | ||
- envoy | ||
vary: | ||
- Origin | ||
x-accel-expires: | ||
- '0' | ||
x-api-warning: | ||
- Please set an API version, for more information please refer to https://docs.cohere.com/versioning-reference | ||
- Version is deprecated, for more information please refer to https://docs.cohere.com/versioning-reference | ||
x-debug-trace-id: | ||
- 4bc0ce4bda5305b5b60ef6268db5e3a7 | ||
x-envoy-upstream-service-time: | ||
- '88' | ||
x-trial-endpoint-call-limit: | ||
- '100' | ||
x-trial-endpoint-call-remaining: | ||
- '98' | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.