以下是使用OpenAI GPT-3.5 Turbo模型编写的一个简单的Python代码,用于生成猴子摘香蕉的故事。

import openai

def complete_prompt(prompt):
    openai.api_key = '你的OpenAI API密钥'
    response = openai.Completion.create(
        engine='text-davinci-003',  # 使用GPT-3.5 Turbo模型
        prompt=prompt,
        max_tokens=100,  # 生成的最大长度限制
        n=1,  # 仅返回一个生成的结果
        stop=None,  # 默认无停止标记
        temperature=0.7,  # 控制生成文本的随机性
        frequency_penalty=0.0,  # 控制生成文本的一致性
        presence_penalty=0.0  # 控制生成文本的多样性
    )
    return response.choices[0].text.strip()

def monkey_story():
    prompt = 'Once upon a time, in a forest, there was a monkey.\n\n' \
             'The monkey loved eating bananas, but there was a problem. The bananas were too high up in the trees for the monkey to reach.\n\n' \
             'One day, the monkey came up with a clever idea. It decided to use a long stick to try and grab the bananas.\n\n' \
             'The monkey started searching for a suitable stick that was long enough to reach the bananas.\n\n' \
             'After a while, the monkey found a perfect stick and climbed up the tree with it.\n\n' \
             'Using the stick, the monkey reached out towards the bunch of bananas, but it was just out of reach.\n\n' \
             'Finally, the monkey had an idea. It tied one end of the stick to its tail and used the other end to push the bananas closer.\n\n' \
             'With a bit of effort, the monkey managed to grab the bananas and pulled them close enough to eat.\n\n' \
             'The monkey happily enjoyed its delicious meal of bananas and felt proud of its clever solution.\n\n' \
             'From that day on, whenever the monkey wanted to eat bananas, it used the same technique and always succeeded.'
             
    completed_story = complete_prompt(prompt)
    print(completed_story)

monkey_story()

你需要在代码中替换你的OpenAI API密钥为你从OpenAI官网获取的API密钥。该代码定义了一个complete_prompt函数,用于调用GPT-3.5 Turbo模型生成完整的故事。monkey_story函数使用预定义的故事开头作为输入并调用complete_prompt函数来生成完整的故事。最后,它打印出生成的故事文本。

Python代码生成猴子摘香蕉的故事 - 使用OpenAI GPT-3.5 Turbo

原文地址: https://www.cveoy.top/t/topic/bW5E 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录