How to use the inference API correctly?

#35
by gbhall - opened

Sorry for unrelated question, but is it possible to use command-r-+ with hf inference api with pro subscription although it is 103B?

Okay I got this to work after I read the docs properly and did more research.

Out of the box it's just a model. What I'm after is a chat-like template. Hugging Face offers something called TGI (Text Generation Inference): https://huggingface.co/docs/text-generation-inference/en/index

To use it, you need to specify it in a request like so using OpenAI's chat completion API: https://huggingface.co/blog/llama3#how-to-prompt-llama-3

from openai import OpenAI

# initialize the client but point it to TGI
client = OpenAI(
    base_url="/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2FCohereForAI%2Fc4ai-command-r-plus" + "/v1/",  # replace with your endpoint url
    api_key="<HF_API_TOKEN>",  # replace with your token
)
chat_completion = client.chat.completions.create(
    model="tgi",
    messages=[
        {"role": "user", "content": "Why is open-source software important?"},
    ],
    stream=True,
    max_tokens=500
)

# iterate and print stream
for message in chat_completion:
    print(message.choices[0].delta.content, end="")

This works.

Cohere Labs org

Closing it as it seems resolved!

alexrs changed discussion status to closed

Sign up or log in to comment