Program that translates text using openai. The program translates the text successfully but there is more information given in the output than needed

huangapple go评论139阅读模式
英文:

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函数来获得所需的输出。以下是修改后的函数:

  1. import openai
  2. def translate_text(text, source_language, target_language):
  3. prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"
  4. response = openai.ChatCompletion.create(
  5. model="gpt-3.5-turbo",
  6. messages=[
  7. {"role": "system", "content": "You are a helpful assistant that translates text."},
  8. {"role": "user", "content": prompt}
  9. ],
  10. max_tokens=150,
  11. n=1,
  12. stop=None,
  13. temperature=0.5,
  14. )
  15. # Extract only the translated text from the response
  16. translation = response.choices[0].message.content.strip()
  17. # Remove square brackets and quotes from the translated text
  18. translation = translation.replace('[', '').replace(']', '').replace(''', '').strip()
  19. 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}"

  1. response = openai.ChatCompletion.create(
  2. model="gpt-3.5-turbo",
  3. messages=[
  4. {"role": "system", "content": "You are a helpful assistant that translates text."},
  5. {"role": "user", "content": prompt}
  6. ],
  7. max_tokens=150,
  8. n=1,
  9. stop=None,
  10. temperature=0.5,
  11. )
  12. translation = response.choices[0].message.content.strip()
  13. return translation

Below I am calling the function and setting up the output:

  1. slide = presentation.slides.add_slide(slide_layout)
  2. shapes = slide.shapes
  3. answers = [row[3], row[4], row[5], row[6]]
  4. body_shape = shapes.placeholders[1]
  5. tf = body_shape.text_frame
  6. t0 = translate_text(answers, 'auto', 'fr').translate(answers[0])
  7. tf.text = "A. " + answers[0] + " <-- Translation --> " + ''.join([str(t0)])
  8. slide_count += 1
  9. 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.

huangapple
  • 本文由 发表于 2023年7月17日 13:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定