OpenAI API 在对话中继续对话

Cha*_*ade 56 python openai-api

我正在使用 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)
Run Code Online (Sandbox Code Playgroud)

这会产生以下格式的俳句:

{
  "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
  }
}
Run Code Online (Sandbox Code Playgroud)

这太棒了。但是,如果我现在想要求“再给我写一封信”怎么办?如果我使用 openAI Playground 聊天或 chatGPT,我就能够继续对话。我想通过我的 python 脚本来做到这一点。我注意到我收到了id回复。我可以用它来继续我的谈话吗?

Sta*_*hen 19

OpenAI 现在正式发布了“gpt-3.5-turbo”模型。这是一些示例代码: https: //github.com/stancsz/chatgpt

是官方文档

import os
import openai


class ChatApp:
    def __init__(self):
        # Setting the API key to use the OpenAI API
        openai.api_key = os.getenv("OPENAI_API_KEY")
        self.messages = [
            {"role": "system", "content": "You are a coding tutor bot to help user write and optimize python code."},
        ]

    def chat(self, message):
        self.messages.append({"role": "user", "content": message})
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=self.messages
        )
        self.messages.append({"role": "assistant", "content": response["choices"][0]["message"].content})
        return response["choices"][0]["message"]
Run Code Online (Sandbox Code Playgroud)

测试日志

how are you? {
  "content": "I'm just a computer program, so I don't have feelings, but I'm functioning properly and ready to help you with any coding questions you have!",
  "role": "assistant"
}
I want to know how to learn python quickly? {
  "content": "There are several ways to learn Python quickly. Here are some tips that may help:\n\n1. Start with the basics: Before jumping into complex topics, ensure that you have a strong foundation in the basics of Python programming such as variables, data types, variable assignment, loops, functions, etc. You can find many resources online to learn these basics.\n\n2. Practice coding regularly: Regular practice is the key to learn Python quickly. You can start by solving small coding challenges or writing small programs on a daily basis.\n\n3. Use interactive platforms: There are several interactive platforms available online, such as Codecademy, HackerRank, and DataCamp, that can help you learn and practice Python.\n\n4. Learn important libraries: Python has a wide range of powerful libraries that can save you time and effort in programming. Examples include NumPy, Pandas, Matplotlib, Scikit-learn, etc. Learning these libraries can help you accelerate your learning and improve your productivity.\n\n5. Participate in coding communities: Participating in online coding communities can help you learn more quickly by interacting with other developers and getting feedback on your code. You can join forums or groups on social media platforms like Reddit and Facebook.\n\nRemember that learning any programming language takes time and effort. However, with consistent practice and a commitment to improving your skills, you can become proficient in Python in a relatively short amount of time.",
  "role": "assistant"
}
What are the best resources for this? {
  "content": "There are many resources available for learning Python quickly. Here are some of the best ones:\n\n1. Coursera: Coursera offers a wide range of Python courses that are designed for beginners and those with some programming experience. These courses cover everything from basic programming concepts to advanced topics like machine learning and data analysis.\n\n2. Codecademy: Codecademy is an interactive learning platform that offers free and paid Python courses. It provides hands-on coding experience and covers the essential Python skills for beginners.\n\n3. Udemy: Udemy is an online learning platform that offers a wide variety of Python courses. It provides both free and paid courses, covering various Python skills and applications, from web development to data science.\n\n4. Automate the Boring Stuff with Python: This is a free online book that covers the basics of Python programming and includes practical examples and projects that you can work on, such as web scraping and automating tasks.\n\n5. Python for Everybody: Python for Everybody is a free course offered by the University of Michigan. It covers the basics of Python programming and is suitable for beginners with no prior programming experience.\n\n6. Python documentation: The official Python documentation is an excellent resource for learning Python. It provides clear and detailed explanations of Python concepts and syntax, as well as examples and code snippets.\n\n7. Python community: The Python community is very active and supportive. You can find help, tutorials, and examples online through various forums, social media groups, and developer communities like Stack Overflow and GitHub.\n\nThese resources can help you learn Python quickly and efficiently, but remember that consistent practice and hands-on experience are key to becoming proficient in Python.",
  "role": "assistant"
}
what should I learn first? {
  "content": "If you are new to programming, you should start with the basic concepts of Python. The following are some of the key concepts to learn first:\n\n1. Variables and Data types: Learn how to create variables and assign values to them. Python has different types of data, such as numbers, strings, and lists.\n\n2. Control Structures: Learn how to control program flow using if/else statements, loops (for and while loops), and basic logic concepts.\n\n3. Functions: Learn how to create functions and pass arguments to them. Functions allow you to segment your code into modular pieces that can be reused throughout your program.\n\n4. Input and Output: Learn how to accept input from users and output results to the screen or file.\n\n5. File handling: Learn how to read/write data from/to files.\n\n6. Libraries: Learn about built-in Python libraries, such as math and random, and how to import external libraries for more advanced functionality.\n\nOnce you have mastered the basics, you can move on to more advanced topics like object-oriented programming, web development, data science, and machine learning.",
  "role": "assistant"
}

Run Code Online (Sandbox Code Playgroud)

  • `stop=["\n"]` 停止序列有点复杂。我建议暂时不要完全使用它。https://community.openai.com/t/i-never-understood-how-stop-sequences-work/5937 (3认同)
  • 感谢您提供出色的代码。我只是不知道将以前的消息添加到新请求是否会增加每次基于(输入+输出令牌)计算的成本!这也会影响生成的代币数量,对吧?由于我们在使用“gpt-3.5-turbo”时有 2048 的限制 (3认同)
  • 这与@Special1st 的答案相同 - 即。仍然自己管理上下文并在每条消息上重新发送它。 (3认同)

小智 16

其实你可以做你想做的事,很简单。只需向 openai 输入提供之前对话的一部分即可。

prompt = "chat message 1\n" + "chat message2\n" + ... + "your last message\n"
Run Code Online (Sandbox Code Playgroud)

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

stop=["\n"]
Run Code Online (Sandbox Code Playgroud)

这里“\n”将用作消息之间​​的分隔符。

  • 以大量的代币/金钱重新发送大量积压的聊天为代价...我可以看到这对 OpenAI 来说比为会话 id 字段添加适当的支持更方便... (16认同)
  • 实际上,费用与模型必须处理的上下文量有关。“会话维护”与成本关系不大,而且会很方便......但他们仍然要向您收取会话大小的费用!如果您不需要上下文,是否会“增加”成本 (3认同)

Pra*_*iel 7

修改Stan Chan的代码以处理可发送的最大数量的对话、使用相同的输入重复相同的答案、捕获率限制错误并更新新信息,例如当前时间和日期。

from os import environ
from collections import deque
from datetime import datetime
from functools import lru_cache

import openai



# Inspired by Stan Chen's code: https://github.com/stancsz/chatgpt
environ["OPENAI_API_KEY"] = "KEYS FROM SOMEWHERE .env"
CHAT_MODEL = "gpt-3.5-turbo"
PROMPT = """Your name is Kim. A kind and friendly AI assistant that answers in \
    a short and concise answer. Give short step-by-step reasoning if required."""


class Chat:
    def __init__(self, converstion_limit: int = 8):

        # number of chats to remember
        self.messages_queue = deque(maxlen=converstion_limit)

    @lru_cache(maxsize=518)
    def chat(self, message: str) -> str:
        self.messages_queue.append({"role": "user", "content": message})

        try:
            prompty = {
                "role": "user",
                "content": f"{PROMPT} Today is {datetime.now(): %A %d %B %Y %H:%M}",
            }
            response = openai.ChatCompletion.create(
                model=CHAT_MODEL, messages=[prompty, *self.messages_queue]
            )

            reply = response["choices"][0]["message"].content

            self.messages_queue.append({"role": "assistant", "content": reply})
        except openai.error.RateLimitError:
            reply = "I am currently overloaded with other requests."

        return reply

Run Code Online (Sandbox Code Playgroud)

结果: 在此输入图像描述

更新

我发现的最好的解决方案是使用langchain

from os import environ
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain import OpenAI, LLMChain, PromptTemplate

environ["OPENAI_API_KEY"] = "KEYS FROM SOMEWHERE .env"
template = """You are a mathematician. Given the text of question, it is your job to write an answer that question with example.
{chat_history}
Human: {question}
AI:
"""
prompt_template = PromptTemplate(input_variables=["chat_history","question"], template=template)
memory = ConversationBufferMemory(memory_key="chat_history")

llm_chain = LLMChain(
    llm=OpenAI(),
    prompt=prompt_template,
    verbose=True,
    memory=memory,
)

llm_chain.run("What is 4 + 3?")

result = llm_chain.run("add 7 to it")
print(result)

Run Code Online (Sandbox Code Playgroud)


小智 3

响应ID中的 用来标识响应所针对的特定查询。userOpenAI 专门使用请求正文中根据您的身份建议提供的字段来监控和检测滥用行为,如其文档中所述。

如果您想生成不同的结果,可以增加temperature请求中的字段,然后再次运行即可。有些工作需要涉及如何设计提示。更多信息请参考OpenAI文档。OpenAI 文档