mogaio's picture
Upload model_api_demo.py
ba98dc6 verified
import requests
chat_history = [] # Stores the conversation history
API_URL = "API_ENDPOINT_URL"
headers = {
"Accept" : "application/json",
"Authorization": "Bearer API_KEY",
"Content-Type": "application/json"
}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
def chat(user_q):
try:
chat_history.append(user_q) # Append user query to chat history
output = query({
"inputs": chat_history, # Send chat history as input data
"parameters": {}
})
generated_reply = output[0]['generated_reply'] # Extract generated reply
chat_history.append(generated_reply) # Append generated reply to chat history
except Exception as ex:
print(ex)
chat(user_q) # Re-run the chat function with the same user query
return generated_reply # Return the generated reply
user_q = '<user>: What are the most visited places in Dublin?' # User query
reply = chat(user_q) # Initiate conversation
user_q = '<user>: Which place is the oldest?' # Next user query
reply = chat(user_q) # Continue conversation
user_q = '<user>: Who built it?' # Next user query
reply = chat(user_q) # Continue conversation
user_q = '<user>: Do you recommend any other places to visit?'
reply = chat(user_q)
user_q = '<user>: What is the average budget for a one-week visit?'
reply = chat(user_q)
print(chat_history) # Print the entire conversation history