# Azure OpenAI GPT-4 on Streamlit connection error

**URL:** <https://discuss.streamlit.io/t/azure-openai-gpt-4-on-streamlit-connection-error/73772>\
**Category:** Using Streamlit\
**Tags:** gpt4, azure, connection, discussion\
**Created:** [July 1, 2024, 12:42am UTC](https://discuss.streamlit.io/t/azure-openai-gpt-4-on-streamlit-connection-error/73772 "2024-07-01T00:42:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Margaux1](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/margaux1/32/45586_2.png) [@Margaux1](https://discuss.streamlit.io/u/Margaux1)\
**Post date:** [July 1, 2024, 12:42am UTC](https://discuss.streamlit.io/t/azure-openai-gpt-4-on-streamlit-connection-error/73772/1 "2024-07-01T00:42:21Z")

</div>

I have the following basic function to work with Azure GPT-4.

def get\_openai\_answer(input):

```
user_prompt = f""" {input}
...
"""

client_us = AzureOpenAI(
api_key="xxx",
api_version="2023-10-01-preview",
azure_endpoint = "xxx"
)

response = client_us.chat.completions.create(
    model="gpt-4", 
    messages=[
        {"role": "system", "content": "you are an expert"},
        {"role": "user", "content": user_prompt}
    ]
)

# Get the response
answer = response.choices[0].message.content

return answer

```

It works outside of Streamlit, but once add my code to my Streamlit app, I get an error when inputting the request: **"openai.APIConnectionError: Connection error. "**

# import

import streamlit as st  
from openai import AzureOpenAI

def get\_openai\_answer(input):

```
user_prompt = f""" {input}
...
"""

client_us = AzureOpenAI(
api_key="xxx",
api_version="2023-10-01-preview",
azure_endpoint = "xxx"
)

response = client_us.chat.completions.create(
    model="gpt-4", 
    messages=[
        {"role": "system", "content": "you are an expert"},
        {"role": "user", "content": user_prompt}
    ]
)

# Get the response
answer = response.choices[0].message.content

return answer

```

# Write intro text

st.title(“helpful engine”)

# Get input

user\_input = st.text\_input("Describe the question: ")

# Wait until click

if st.button(“Search”):  
answer = get\_openai\_answer(input)  
# Write answer  
st.write(answer)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [December 28, 2024, 12:43am UTC](https://discuss.streamlit.io/t/azure-openai-gpt-4-on-streamlit-connection-error/73772/2 "2024-12-28T00:43:21Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
