taxibot / app.py
zishan's picture
added key
5f6773a verified
import openai
import os
import time
import gradio as gr
api_key = os.getenv("OPENAI_API_KEY")
# Initialize the client
client = openai.OpenAI(api_key=api_key)
# Step 1: Create an Assistant
assistant = client.beta.assistants.create(
name="Taxi Assistant",
instructions="You are an intelligent assistant designed to gather specific pieces of information from users through a friendly and engaging conversation."
"\n\n Your goal is to ensure that you collect all the necessary details required for booking a taxi by asking follow-up questions based on the user's responses. If the user provides incomplete information, politely ask for the missing details. If the user's response is off-topic, gently steer the conversation back to the relevant questions. Remember to thank the user for their responses and provide encouragement to create a positive interaction experience."
"\n\n The information required from the user is:"
"\n\n 1. Begin the conversation by introducing yourself and stating the purpose of the information collection."
"\n\n 2. Clearly list the pieces of information you need to collect."
"\n\n 3. For each piece of information:"
"\n\n a. Ask a direct question to the user."
"\n\n b. If the user's response is incomplete, ask a follow-up question to gather the missing details."
"\n\n c. If the user provides all the required information for a point, acknowledge it and move on to the next piece of information."
"\n\n 4. Once all information is collected, summarize the details gathered to confirm with the user."
"\n\n 5. Thank the user for their time and cooperation, and inform them of the next steps if applicable."
"\n\n Remember, your interactions should be polite, concise, and focused on gathering the necessary information efficiently while maintaining a friendly tone."
,
model="gpt-3.5-turbo",
#file_ids=[file.id],
tools=[{"type": "code_interpreter"}]
)
# Step 2: Create a Thread
thread = client.beta.threads.create()
def main(query):
# Step 3: Add a Message to a Thread
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=query
)
# Step 4: Run the Assistant
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="[Your Turn]: Start the conversation by introducing yourself and asking the first question based on the information you need to collect."
)
while True:
# Wait for 5 seconds
time.sleep(1)
# Retrieve the run status
run_status = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
# If run is completed, get messages
if run_status.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
response = ""
# Loop through messages and print content based on role
for msg in messages.data:
role = msg.role
content = msg.content[0].text.value
response += f"{role.capitalize()}: {content}\n\n"
return response+"\n\n"
else:
continue
# Create a Gradio Interface
iface = gr.Interface(fn=main, inputs="textbox", outputs="textbox", title="Chatbot")
iface.launch()