File size: 1,487 Bytes
ba98dc6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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
|