Skip to content
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

return usage for all providers - As OpenAI does #11

Closed
ishaan-jaff opened this issue Jul 28, 2023 · 5 comments
Closed

return usage for all providers - As OpenAI does #11

ishaan-jaff opened this issue Jul 28, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@ishaan-jaff
Copy link
Contributor

ishaan-jaff commented Jul 28, 2023

OpenAI returns usage for requests, but other providers - eg. Cohere does not.

{
  "id": "chatcmpl-7hPIZUYBst5jQA4odnYgxgZ9iPZ51",
  "object": "chat.completion",
  "created": 1690579707,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! I'm an AI language model, so I don't have feelings, but I'm here to help you with any questions or conversations you have. How can I assist you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 38,
    "total_tokens": 51
  }

Current Cohere return val from litellm - it's missing usage - which I need to calculate costs $

{'choices': [{'finish_reason': 'stop', 'index': 0, 'message': {'content': cohere.Generation {
	id: 3369b9a7-5755-4a4b-9da6-1ebab7c7924a
	prompt: Hello, how are you?
	text:  I am doing well, thank you. How can I help you today?
	likelihood: None
	finish_reason: None
	token_likelihoods: None
}, 'role': 'assistant'}}]}
@krrishdholakia
Copy link
Contributor

lol exact conclusion i landed on in #10

@krrishdholakia
Copy link
Contributor

@ishaan-jaff doesn't the cohere response look broken -> it should have just returned the text. seems a bit weird to just nest the cohere object in there.

@krrishdholakia
Copy link
Contributor

For anthropic / replicate i just mapped the text output:

elif model in anthropic_models:
    #anthropic defaults to os.environ.get("ANTHROPIC_API_KEY")
    prompt = f"{HUMAN_PROMPT}" 
    for message in messages:
      if "role" in message:
        if message["role"] == "user":
          prompt += f"{HUMAN_PROMPT}{message['content']}"
        else:
          prompt += f"{AI_PROMPT}{message['content']}"
      else:
        prompt += f"{HUMAN_PROMPT}{message['content']}"
    prompt += f"{AI_PROMPT}"
    anthropic = Anthropic()
    completion = anthropic.completions.create(
        model=model,
        prompt=prompt,
        max_tokens_to_sample=max_tokens
    )
    new_response = {
      "choices": [
        {
          "finish_reason": "stop",
          "index": 0,
          "message": {
              "content": completion.completion,
              "role": "assistant"
          }
        }
      ]
    }
    print(f"new response: {new_response}")
    response = new_response

@krrishdholakia
Copy link
Contributor

version 0.1.341 now returns token usage across all providers. Where possible it uses the providers own tokenizer (E.g. anthropic) else it defaults to tiktoken.

@krrishdholakia
Copy link
Contributor

commit: 7575d7e

@krrishdholakia krrishdholakia self-assigned this Aug 5, 2023
@krrishdholakia krrishdholakia added the enhancement New feature or request label Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants