I am trying to apply a chatbot using the Azure OpenAI service with my own data. I want to give it specific instructions to personalize it. Since it utilizes ChatGPT, I can communicate with it in almost any language. Therefore, I have instructed it to respond to user queries in the language they were asked. Additionally, I have given it a name. However, it always responds in English to the first question (for subsequent questions it mostly changes), and when I ask for its name, it says it does not have one. This suggests that my “system message” is not being considered at all. I would be grateful for any help.
import os
import openai
import dotenv
import re
dotenv.load_dotenv()
endpoint = os.environ.get("AZURE_OAI_ENDPOINT")
api_key = os.environ.get("AZURE_OAI_KEY")
deployment = os.environ.get("AZURE_OAI_DEPLOYMENT")
client = openai.AzureOpenAI(
azure_endpoint=endpoint,
api_key=api_key,
api_version="2024-02-01",
)
# Initial system message
system_message = {
"role": "system",
"content": "Sei un assistente virtuale chiamato Jonathan. Rispondi sempre nella stessa lingua della domanda dell'utente.",
}
def get_response(user_query):
completion = client.chat.completions.create(
model=deployment,
temperature=0.5,
max_tokens=1000,
messages=[
system_message,
{
"role": "user",
"content": user_query,
},
],
extra_body={
"data_sources": [
{
"type": "azure_search",
"parameters": {
"endpoint": os.environ["AZURE_SEARCH_ENDPOINT"],
"index_name": os.environ["AZURE_SEARCH_INDEX"],
"authentication": {
"type": "api_key",
"key": os.environ["AZURE_SEARCH_KEY"],
}
}
}
],
}
)
# Extract the response content
response_content = completion.choices[0].message.content
# Remove references like [doc4] using regex
cleaned_response_content = re.sub(r'[docd+]', '', response_content)
return cleaned_response_content
def main():
while True:
# Prompt the user for input
user_query = input("Per favore inserisci la tua domanda (o 'esci' per terminare): ")
if user_query.lower() in ['esci', 'exit', 'quit']:
print("Bye bye!")
break
# Get and print the response
response = get_response(user_query)
print("Response: " + "n" + response + "n")
if __name__ == '__main__':
main()