|
import openai |
|
import os |
|
import time |
|
import gradio as gr |
|
api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
client = openai.OpenAI(api_key=api_key) |
|
|
|
|
|
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", |
|
|
|
tools=[{"type": "code_interpreter"}] |
|
) |
|
|
|
|
|
thread = client.beta.threads.create() |
|
|
|
def main(query): |
|
|
|
message = client.beta.threads.messages.create( |
|
thread_id=thread.id, |
|
role="user", |
|
content=query |
|
) |
|
|
|
|
|
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: |
|
|
|
time.sleep(1) |
|
|
|
|
|
run_status = client.beta.threads.runs.retrieve( |
|
thread_id=thread.id, |
|
run_id=run.id |
|
) |
|
|
|
|
|
if run_status.status == 'completed': |
|
messages = client.beta.threads.messages.list( |
|
thread_id=thread.id |
|
) |
|
response = "" |
|
|
|
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 |
|
|
|
|
|
iface = gr.Interface(fn=main, inputs="textbox", outputs="textbox", title="Chatbot") |
|
iface.launch() |