philschmid / easyllm

Home Page:https://philschmid.github.io/easyllm/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bedrock.ChatCompletion.create Raises ValidationError for Non-Integer Token Values in Python 3.9

5c0rp opened this issue · comments

Python 3.9

Code to reproduce:

import os
# set env for prompt builder
os.environ["BEDROCK_PROMPT"] = "anthropic"
os.environ["AWS_REGION"] = "us-east-1"
os.environ["AWS_ACCESS_KEY_ID"] = "xxx"
os.environ["AWS_SECRET_ACCESS_KEY"] = "xxx"

from easyllm.clients import bedrock
from easyllm.schema.base import ChatMessage

response = bedrock.ChatCompletion.create(
    model="anthropic.claude-v2",
    messages=[
        ChatMessage(role="user", content="What is 2 + 2?"),
    ],
    temperature=0.9,
    top_p=0.6,
    max_tokens=1024,
    debug=False,
)
print(response)

Result:

Traceback (most recent call last):
  File "<path_to>\main.py", line 27, in <module>
    response = bedrock.ChatCompletion.create(
  File "<path_to>\lib\site-packages\easyllm\clients\bedrock.py", line 208, in create
    usage=Usage(
  File "<path_to>\lib\site-packages\pydantic\main.py", line 159, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for Usage
completion_tokens
  Input should be a valid integer, got a number with a fractional part [type=int_from_float, input_value=2.25, input_type=float]
    For further information visit https://errors.pydantic.dev/2.1/v/int_from_float
total_tokens
  Input should be a valid integer, got a number with a fractional part [type=int_from_float, input_value=11.25, input_type=float]
    For further information visit https://errors.pydantic.dev/2.1/v/int_from_float

Expected result:
{'id': 'hf-iE53Kvwnlr', 'object': 'chat.completion', 'created': 1698683234, 'model': 'anthropic.claude-v2', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '2 + 2 = 4'}, 'finish_reason': 'stop_sequence'}], 'usage': {'prompt_tokens': 9, 'completion_tokens': 2, 'total_tokens': 11}}

Is fixed in #42 and available in 0.6.2