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)