python Openai API持续对话

d6kp6zgx  于 2023-02-11  发布在  Python
关注(0)|答案(4)|浏览(1720)

我正在玩openAi API,我试图继续一个对话。例如:

import openai
openai.api_key = mykey

prompt= "write me a haiku"

response = openai.Completion.create(engine="text-davinci-001",prompt=prompt
        ,max_tokens=50)
print(response)

这将生成以下格式的俳句:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "\n\n\n\nThis world is\nfull of wonders\nSo much to see and do"
    }
  ],
  "created": 1670379922,
  "id": "cmpl-6KePalYQFhm1cXmwOOJdyKiygSMUq",
  "model": "text-davinci-001",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 17,
    "prompt_tokens": 5,
    "total_tokens": 22
  }
}

这很好,但是,如果我现在想让openai“再给我写一个”呢?如果我使用openai playground chat或chatGPT,我可以继续对话。我想通过我的python脚本来完成。我注意到我收到了一个id作为响应。我可以用这个来继续我的对话吗?

s5a0g9ez

s5a0g9ez1#

事实上你可以做你想做的事情,很简单。只要提供给openai输入前面对话的一部分。

prompt = "chat message 1\n" + "chat message2\n" + ... + "your last message\n"

不要忘记在“openai.completion.create”中设置“stop”变量。

stop=["\n"]

此处“\n”将用作消息之间的分隔符。

btxsgosb

btxsgosb2#

响应中的ID用于标识响应所针对的特定查询。请求正文中根据您的身份建议的user字段由OpenAI专门用于监控和检测滥用,如其文档中所述。
如果您想生成不同的结果,可以增加请求中的temperature字段,然后再次运行它。有些工作需要进行到如何设计提示。有关更多信息,请参阅OpenAI文档。OpenAI Documentation

5rgfhyps

5rgfhyps3#

下面是一个如何执行此操作的示例:https://github.com/stancsz/gpt3-convo-mode
我还想建议不要在新的提示符有效载荷中包含太多的提示符和响应的历史背景。如果这样做,你可能会增加API成本。最好的方法是保持对话简短,或者只是记住一些以前的提示符和响应。(在我的例子中,它记住了整个背景)
我仍然很好奇ChatGPT如何实现他们的解决方案,因为GPT-3与ChatGPT不同。

import os
import re
import openai

# Setting the API key to use the OpenAI API
openai.api_key = os.getenv("OPENAI_API_KEY")

# Setting up the logging feature by creating a file with the topic name
topic = "demo"
history_log = 'history/' + re.sub('[^0-9a-zA-Z]+', '', topic) + '.log'
file = open(history_log, "a")

# Initializing the prompt and context variables
prompt = ""
context = ""

while True:
    # Prints '>>' to indicate user input is needed
    print(">>")
    # User input for the prompt
    prompt = input()
    # If the user inputs 'exit', the loop breaks
    if prompt == 'exit':
        break
    # Writes the user's input to the log file
    file.write(prompt)
    # Sends the prompt and context to the OpenAI API
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt="context:" + context + "\n\n" + "prompt:" + prompt,
        temperature=0.7,
        max_tokens=4000,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    # Writes the API's response to the log file
    file.write(response["choices"][0]["text"] + "\n")
    # Prints the API's response
    print(response["choices"][0]["text"] + "\n")
    # Adds the prompt and response to the context variable
    context += "\n".join([context, prompt, response["choices"][0]["text"]])

# Closes the log file
file.close()

控制台日志

>>
givme me a list of 10 programming languages

1. Java
2. C/C++
3. Python
4. JavaScript
5. Ruby
6. PHP
7. Swift
8. Go
9. Rust
10. Kotlin

>>
for each of the programming language above, write a short intro of each

1. Java: Java is a general-purpose, object-oriented programming language developed by Sun Microsystems in 1995. It is a powerful, secure language that can be used to create a wide variety of applications.

2. C/C++: C and C++ are two of the most popular programming languages in the world. C is a powerful, low-level language used for system and application programming, while C++ is an object-oriented language that is used for developing large software applications.

3. Python: Python is an interpreted, high-level, general-purpose programming language. It is a versatile language that can be used to create desktop applications, web applications, data science projects, and more.

4. JavaScript: JavaScript is a high-level, interpreted programming language commonly used to create interactive web applications. It is a popular language that is used by developers all over the world.

5. Ruby: Ruby is an object-oriented programming language designed to be easy to learn and use. It is a popular language used to create web applications and has powerful features such as metaprogramming.

6. PHP: PHP is a popular server-side scripting language used to create dynamic websites and web applications. It is a flexible language that can be used for a variety of tasks.

7. Swift: Swift is a powerful, open-source programming language developed by Apple in 2014. It is a modern language used to create applications for iOS and macOS.

8. Go: Go is a general-purpose programming language developed at Google in 2009. It is a statically typed language that is designed to be easy to read, write, and maintain.

9. Rust: Rust is a modern, low-level programming language developed at Mozilla in 2010. It is a safe, fast language that can be used to create a variety of applications.

10. Kotlin: Kotlin is a statically typed, cross-platform programming language developed at JetBrains in 2011. It is a modern language that is used to create Android and web applications.

>>

您可以继续传递context作为下一个提示符,以保持会话的活动。
我已经把它包括在这篇文章:https://medium.com/@stancsz/keeping-the-gpt-3-context-alive-875e1624adf4
阅读官方文档中的更多信息:https://beta.openai.com/docs/guides/completion/prompt-design

6qftjkof

6qftjkof4#

我尝试了一个非常昂贵的想法,它似乎起作用了。这个想法是to provide the context of previous discussions by enriching your current prompt with the previous promts and responses
请参见下面的示例代码。
'

import re, requests, os
env = os.environ
OPENAI_PUBLIC_KEY = env['OPENAI_PUBLIC_KEY']

public_end_point = 'https://api.openai.com/v1/completions'
headers = {'authorization': f"Bearer {OPENAI_PUBLIC_KEY}"}

#This function provides the context. Note that that it will consume a lot of tokens (input tokens)
def get_last_5_summary_chats(chats):
    res =''
    for index, question_response in enumerate(chats[-5:]):
        res+= f"prompt{index}: {question_response[0]} response{index}: {question_response[1]} "
    if(len(chats)> 3):
        res = "Give short responses only. "+ res
    return res

#Store your chat history in session_chats
session_chats = []

#Set Input Parameters to the endpoint
data = { "model": 'text-davinci-003', "max_tokens": 400, "temperature": 1, "top_p": 0.6}

for ind in range(10):
    prev_context = get_last_5_summary_chats(session_chats)
    prompt = input("Ask your question:\t").strip()
    data['prompt'] = f"{prev_context} {prompt}".strip()
    r = requests.post(public_end_point, headers=headers, json=data)
    public_response = r.json()
    response_text = public_response['choices'][0]['text'].strip()
    print(f"QUESTION:\t{prompt}\n")
    print(f"RESPONSE:\t {response_text}\n\n")
    session_chats.append([prompt, response_text])

'
下面是我从API得到的一个聊天示例。

相关问题