Skip to content

UnicodeEncodeError: 'ascii' codec can't encode character '\u201d' in position 59: ordinal not in range(128) #787

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

Closed
1 task done
Nghiauet opened this issue Nov 11, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@Nghiauet
Copy link

Nghiauet commented Nov 11, 2023

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

I run the code from docs but receive the error
I find the cause of the bug is text load from .env have some special character.

from dotenv import load_dotenv
load_dotenv()
import os
openai.api_key = os.getenv("OPENAI_API_KEY")

and solved with

client = OpenAI(
      api_key="sk...."
)

Seem have something weird when load .env file
My .env file is and I still use it for other code. But dont know this version receive error

I use new .venv of python 3.10.12 only install python-dotenv

To Reproduce

!pip install python-dotenv
!pip install openai

RUN the code snip set

I still can print
print(u"\u201d")

Code snippets

import openai
from openai import OpenAI

from dotenv import load_dotenv
load_dotenv()
import os
openai.api_key = os.getenv("OPENAI_API_KEY")

client = OpenAI()
response = client.chat.completions.create(
  model="gpt-3.5-turbo",
    response_format={ "type": "json_object" },

  messages=[
    {"role": "system", "content": "You are 3translator to translate the input text to destination language the input is the text nees to translate. First: Detect the language of the input text Second: Translate the input text from the detected language in first step to the Vietnamese language Third: Return output only the translated text not add up the description"},
    {"role": "user", "content": "this is a cat"},
  ]
)

OS

ubuntu 22.04.3

Python version

3.10.12

Library version

openai 1.2.3

@Nghiauet Nghiauet added the bug Something isn't working label Nov 11, 2023
@BlueHephaestus
Copy link

I'm not sure if you managed to fix your issue, but it seems to have been caused by the Right Double Quote being somewhere in your env file or in your code, which is quite a buggar to track down. I'm glad you figured it out!

I did notice on trying to run your code that it didn't work however, due to first the model type being used, then the lack of the word "JSON" in the query string. Here's the fixed version of it after the fact if you're still having issues:

import openai
from openai import OpenAI

from dotenv import load_dotenv
load_dotenv()
import os
openai.api_key = os.getenv("OPENAI_API_KEY")

client = OpenAI()
response = client.chat.completions.create(
  model="gpt-3.5-turbo-1106",
    response_format={ "type": "json_object" },

  messages=[
    {"role": "system", "content": "You are 3translator to translate the input text to destination language the input is the text nees to translate. First: Detect the language of the input text Second: Translate the input text from the detected language in first step to the Vietnamese language Third: Return output only the translated text not add up the description, and return it in JSON format."},
    {"role": "user", "content": "this is a cat"},
  ]
)
print(response.choices[0].message.content)

Which produces:

$ python3 test.py 
{
  "translatedText": "đây là một con mèo"
}

If that's solved your problem, feel free to close this issue. Good luck with your project!

@rattrayalex
Copy link
Collaborator

Glad you were able t find the problem!

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