英文:
Program that translates text using openai. The program translates the text successfully but there is more information given in the output than needed
问题
在你的代码中,你可以通过修改translate_text
函数来获得所需的输出。以下是修改后的函数:
import openai
def translate_text(text, source_language, target_language):
prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that translates text."},
{"role": "user", "content": prompt}
],
max_tokens=150,
n=1,
stop=None,
temperature=0.5,
)
# Extract only the translated text from the response
translation = response.choices[0].message.content.strip()
# Remove square brackets and quotes from the translated text
translation = translation.replace('[', '').replace(']', '').replace(''', '').strip()
return translation
这个修改后的函数将返回所需的翻译,不再包含其他选择。你可以像之前一样调用这个函数,得到你期望的输出。
英文:
I am trying to translate text by using an openai in python. I am using gpt-3.5-turbo. I wrote a function to help me with the translation and it is executing and working correctly but gives me more output than I need. Here is the written function:
def translate_text(text, source_language, target_language):
prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that translates text."},
{"role": "user", "content": prompt}
],
max_tokens=150,
n=1,
stop=None,
temperature=0.5,
)
translation = response.choices[0].message.content.strip()
return translation
Below I am calling the function and setting up the output:
slide = presentation.slides.add_slide(slide_layout)
shapes = slide.shapes
answers = [row[3], row[4], row[5], row[6]]
body_shape = shapes.placeholders[1]
tf = body_shape.text_frame
t0 = translate_text(answers, 'auto', 'fr').translate(answers[0])
tf.text = "A. " + answers[0] + " <-- Translation --> " + ''.join([str(t0)])
slide_count += 1
print(t0)
My output looks like the following:
A. Thugs of Bharat <-- Translation --> ['Thugs de Bharat', 'Pirates de l'Inde', 'Thugs de Hindostan', 'Combattants de la mer']
How do I remove the other choices that's listed.
The desired output would be:
A. Thugs of Bharat <-- Translation --> Thugs de Bharat
I tried modifying the function but I have been unsuccessful. I tried modifying the return statement in the function but I also didn't have any luck trying that.
答案1
得分: 1
尝试返回一个包含前一个尝试的返回值的第一个值的列表。示例:previouslist = ['Thugs de Bharat', 'Pirates de l'Inde', 'Thugs de Hindostan', 'Combattants de la mer'] -> [previouslist[0]]。
您可以尝试这种方法,但最好不要剥离其他值,甚至完全不要使用OpenAI进行翻译。更好的方法是使用API进行翻译,您可以使用python3
中的requests
模块来实现。
英文:
Probably try returning a list with the first value of the previous return value you tried. Example: previouslist = ['Thugs de Bharat', 'Pirates de l'Inde', 'Thugs de Hindostan', 'Combattants de la mer'] -> [previouslist[0]].
You could try this method, but it's not a good idea to strip out the other values, and even translating with OpenAI at all. A better idea would be to use an API for translating, you can do that with the requests
module in python3
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论