# OpenAI ChatCompletion example doesn't work

**URL:** <https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819>\
**Category:** API\
**Tags:** chatgpt, api\
**Created:** [August 30, 2023, 1:19pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819 "2023-08-30T13:19:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![abs1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/abs1/32/147883_2.png) [@abs1](https://community.openai.com/u/abs1)\
**Post date:** [August 30, 2023, 1:19pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/1 "2023-08-30T13:19:44Z")

</div>

Looked at related question [here](https://community.openai.com/t/api-openai-chatcompletion-error/82371), but that does not work.

This example is from the [docs](https://platform.openai.com/docs/api-reference/chat/create?lang=python)

```auto
assert openai. __version__ == '0.27.10' # latest as of this question

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo", # engine too does not work (and docs has model)
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ]
)

print(completion.choices[0].message)

```

```auto
InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a

```

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [August 30, 2023, 1:39pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/2 "2023-08-30T13:39:32Z")

</div>

Hi,

This is my working python code

```auto
response = openai.ChatCompletion.create(
        model='gpt-3.5-turbo',
        temperature= 0.5,
        messages=[{"role": "user", "content": prompt}],
    )

```

I don’t have any assert line in mine just a `import openai` also I think you’re using an old openai library mine is

```auto
openai --version
openai 0.27.8

be worth doing an update on it.
```

---

<div class="post-metadata">

**Author:** ![abs1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/abs1/32/147883_2.png) [@abs1](https://community.openai.com/u/abs1)\
**Post date:** [August 30, 2023, 2:01pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/4 "2023-08-30T14:01:25Z")

</div>

Thanks. Tried with this version too (0.27.8) - still doesn’t work.

PS: Please note that the version in my original post (0.27.10) is newer than yours (0.27.8)

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [August 30, 2023, 2:07pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/5 "2023-08-30T14:07:25Z")

</div>

Fair point on the 2.10 and 2.8.

The issue you are facing is one from some time ago, when the API was managed via engines, is there any more to your code? i.e. the setup code.

---

<div class="post-metadata">

**Author:** ![abs1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/abs1/32/147883_2.png) [@abs1](https://community.openai.com/u/abs1)\
**Post date:** [August 30, 2023, 2:31pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/6 "2023-08-30T14:31:08Z")

</div>

I’m using this setup code

```auto
openai.api_key = KEY
openai.api_base = ENDPOINT # https://...
openai.api_type = 'azure'
openai.api_version = '2023-05-15' # is this OK?

```

I’m using the version from [this](https://github.com/openai/openai-python/blob/main/README.md) readme.

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [August 30, 2023, 2:47pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/7 "2023-08-30T14:47:21Z")

</div>

ok, let me give you my entire demo code an see if this runs for you, you can then utilise that as a jumping off point, note that I make use of environmental variable set with the export command, if you wish the API key to be stored in your enviroment across reboots and session you can use `echo 'export OPENAI_API_KEY=your_api_key' >> ~/.bashrc`

```auto
import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {
      "role": "user",
      "content": "what is 5+5?"
    }
  ],
  temperature=0.5
)
print(response.choices[0].message)

```

---

<div class="post-metadata">

**Author:** ![abs1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/abs1/32/147883_2.png) [@abs1](https://community.openai.com/u/abs1)\
**Post date:** [August 30, 2023, 3:19pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/8 "2023-08-30T15:19:09Z")

</div>

Thanks for your help.  
Turned out to be an issue with the `deployment_id`.

---

<div class="post-metadata">

**Author:** ![novaphil](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/novaphil/32/130934_2.png) [@novaphil](https://community.openai.com/u/novaphil)\
**Post date:** [August 30, 2023, 7:06pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/9 "2023-08-30T19:06:55Z")

</div>

For anyone else that finds this: Azure has a slightly different API format.

Pro Tip: The Azure Playground has a “View Code” link that will give you the correct code in several languages:

```python
#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = "https://<your-compnay>.openai.azure.com/"
openai.api_version = "2023-07-01-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
  engine="<your-deployment-name>",
  messages = [{"role":"user","content":"Hello!"}])

```

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [August 30, 2023, 7:17pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/10 "2023-08-30T19:17:38Z")

</div>

Ahhhh, that makes a lot of sense now, thank you very much, Nova.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [August 31, 2023, 5:48am UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/11 "2023-08-31T05:48:38Z")

</div>

Protip 2: only specifying the latest API versions will get you function calling, the latest is `2023-08-01-preview`

as of `2023-07-01-preview`, only GPT-4 could use functions.

You can also explore [2023-08-01-preview](https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/preview/2023-08-01-preview/inference.json), and see a extensionsChatCompletionsRequest with things like `"dataSources":` alongside messages, and the citations output format demonstrated in Bing AI chat.

---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [December 17, 2023, 7:51pm UTC](https://community.openai.com/t/openai-chatcompletion-example-doesnt-work/340819/12 "2023-12-17T19:51:24Z")

</div>


