[Python] OpenAI API continuing conversation in a dialogue

Discussão em 'Python' iniciado por Stack, Julho 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am playing around with the openAI API and I am trying to continue a conversation. For example:

    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)


    This produces a Haiku in the following format:

    {
    "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
    }
    }


    Which is great. However, what if I now want to ask to "write me another"? If I use the the openAI playground chat or chatGPT, I am able to continue the conversation. I would like to do this via my python script. I notice I receive an id in response. Can I use this somehow to continue my conversation?

    Continue reading...

Compartilhe esta Página