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

Airforce utf-8 decode error #2480

Open
HongyiHank opened this issue Dec 13, 2024 · 3 comments
Open

Airforce utf-8 decode error #2480

HongyiHank opened this issue Dec 13, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@HongyiHank
Copy link

Bug description
While I using this code

import g4f

provider = g4f.Provider.Airforce

messages = [
    {"role": "system", "content": "Your name is Hank"},
    {"role": "user", "content": "hellow"}
]

response = g4f.ChatCompletion.create(
    model="gpt-4o",
    messages=messages,
    provider=provider
)

print(response)

I'll get an error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 1: invalid start byte

Environment

  • python version 3.12.5

Additional context
complete error message:

C:\Users\hongyihank\OneDrive\doc>py test.py
Traceback (most recent call last):
  File "C:\Users\hongyihank\OneDrive\doc\test.py", line 10, in <module>
    response = g4f.ChatCompletion.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hongyihank\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\__init__.py", line 55, in create
    return result if stream else ''.join([str(chunk) for chunk in result])
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hongyihank\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\providers\asyncio.py", line 53, in to_sync_generator
    yield loop.run_until_complete(await_callback(gen.__anext__))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hongyihank\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\hongyihank\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\providers\asyncio.py", line 38, in await_callback
    return await callback()
           ^^^^^^^^^^^^^^^^
  File "C:\Users\hongyihank\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\Provider\Airforce.py", line 271, in create_async_generator
    async for result in cls.generate_text(model, messages, max_tokens, temperature, top_p, stream, api_key, proxy):
  File "C:\Users\hongyihank\AppData\Local\Programs\Python\Python312\Lib\site-packages\g4f\Provider\Airforce.py", line 236, in generate_text
    result = await response.json()
             ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hongyihank\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiohttp\client_reqrep.py", line 1260, in json
    return loads(stripped.decode(encoding))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 1: invalid start byte
@HongyiHank HongyiHank added the bug Something isn't working label Dec 13, 2024
@Arken1xs
Copy link

I have the same issue with Airforce provider. Similar issue will arise if you will try to use GizAI provider:

    response = await client.chat.completions.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               
  File "\g4f\client\__init__.py", line 163, in async_iter_append_model_and_provider
  
    async for chunk in response:
    
  File "\g4f\client\__init__.py", line 118, in async_iter_response
     
    async for chunk in response:
    
  File "\g4f\Provider\GizAI.py", line 73, in create_async_generator  
  
    result = await response.json()
             ^^^^^^^^^^^^^^^^^^^^^
             
  File "\aiohttp\client_reqrep.py", line 1199, in json
  
    raise ContentTypeError(
    
aiohttp.client_exceptions.ContentTypeError: 0, message="Attempt to decode JSON with unexpected mimetype: ', url='https://app.giz.ai/api/data/users/inferenceServer.infer"```

@Arken1xs
Copy link

For some reason, response.headers from Airforce always contains this:
'Content-Encoding': 'zstd'
ImHex detects the binary content of response as "Zstandard compressed data format" as well.

@Arken1xs
Copy link

Arken1xs commented Dec 14, 2024

This issue is related only to non-streaming response, so I guess that Airforce is just returning stream by default.

@HongyiHank use this code to work with Airforce (that is the basic example):

answer = ""
async for chunk in response:
   if chunk.choices and chunk.choices[0].delta.content:
      answer += chunk.choices[0].delta.content
 history.append({"role": "assistant", "content": answer})
 print(answer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants