genai-impact / ecologits

🌱 EcoLogits tracks the energy consumption and environmental footprint of using generative AI models through APIs.
https://ecologits.ai/
Mozilla Public License 2.0
63 stars 8 forks source link

Add Cohere provider #27

Closed samuelrince closed 5 months ago

samuelrince commented 6 months ago

Description

Add Cohere LLM provider.

Solution

Cohere has its own API and python client.

Available models: https://docs.cohere.com/docs/models

Code example:

import cohere

client = cohere.Client('<<apiKey>>')

response = client.chat(
    chat_history=[
        {"role": "USER", "message": "Who discovered gravity?"},
        {"role": "CHATBOT", "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton"}
    ],
    message="What year was he born?",
    connectors=[{"id": "web-search"}]
)

print(response)
adrienbanse commented 5 months ago

Started working on this in feat/cohere, works with

from ecologits import EcoLogits
import cohere

EcoLogits.init()

client = cohere.Client()
chat = client.chat(
    message="Hello!", 
    max_tokens=100, 
    model = "command-light"
)

print(chat.text)
print(chat.impacts)
adrienbanse commented 5 months ago

Note: streams work as

from ecologits import EcoLogits
import cohere

EcoLogits.init()
co = cohere.Client()
stream = co.chat_stream(
    message="Tell me a short story", 
    model="command-light", 
    max_tokens=50
)

for event in stream:
    if event.event_type == "text-generation":
        print(event.text, end='')

    if event.event_type == "stream-end":
        print("\n")
        print(event.impacts)

Similarly as for Anthropic, output tokens are only returned at the end of the stream