OpenAI has revolutionized the way we interact with artificial intelligence by introducing the Assistants API. This powerful tool allows users to create personalized AI assistants that can integrate custom data, execute Python code, and interact with external APIs. In this blog, we will explore how to create and use these assistants effectively using both the Playground UI and Python APIs. Whether you're a developer looking to enhance your applications or a business owner aiming to streamline operations, OpenAI's Assistants offer a versatile solution.
The Playground UI provides a user-friendly interface for creating AI assistants without the need for extensive coding knowledge. Here’s a step-by-step guide:
gpt-4-1106-preview
to enable data uploads.For those who prefer a programmatic approach, the Python APIs offer greater flexibility and control. Here's how you can set up an assistant using Python:
pip install --upgrade openai
to install the necessary SDK.assistant.py
and Add Your CSV File: Prepare your script and data file for integration.from openai import OpenAI
# Update with your API key
client = OpenAI(api_key='YOUR_API_KEY_HERE')
# Open the CSV file in 'read binary' (rb) mode, with the 'assistants' purpose
file = client.files.create(
file=open('TravelPreferences.csv', 'rb'),
purpose='assistants'
)
# Create and configure the assistant
assistant = client.beta.assistants.create(
name='Terrific Travels',
instructions='You are a travel agent specializing in world travel. Your job is to suggest itineraries and give travel tips based on provided data.',
model='gpt-4-1106-preview',
tools=[{'type': 'retrieval'}],
file_ids=[file.id]
)
# Create a thread for the conversation
thread = client.beta.threads.create()
# Create the user message and add it to the thread
message = client.beta.threads.messages.create(
thread_id=thread.id,
role='user',
content='I'd like help planning a new trip based on criteria for Amber. I'd prefer to visit a country I haven't been to yet. What would you suggest?',
)
# Create the Run, passing in the thread and the assistant
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
# Periodically retrieve the Run status
while run.status != 'completed':
keep_retrieving_run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
print(f'Run status: {keep_retrieving_run.status}')
if keep_retrieving_run.status == 'completed':
print('\nRun completed')
break
# Retrieve messages added by the Assistant to the thread
all_messages = client.beta.threads.messages.list(
thread_id=thread.id
)
# Print the messages from the user and the assistant
print('######################################################\n')
print(f'USER: {message.content}')
print(f'ASSISTANT: {all_messages.data[0].content}')
python assistant.py
Execute the script to start the assistant, which will periodically check the run status and print the conversation messages once completed.
OpenAI Assistants provide a robust framework for creating intelligent, personalized interactions. By leveraging custom data, real-time code execution, and external APIs, these assistants can significantly enhance your application's capabilities. Whether you're using the Playground UI for a quick setup or diving into Python for deeper customization, the potential applications are vast and varied.
By following these steps, you can create versatile and efficient assistants that cater to your specific needs and use cases, enhancing your application's interactivity and functionality.
Sign up to learn more about how raia can help
your business automate tasks that cost you time and money.