OpenAI Assistants: How to Create and Use Them Effectively

Date Icon
October 24, 2024

Introduction to OpenAI Assistants

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.

Creating an Assistant Using the Playground UI

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:

Create the Assistant

  1. Navigate to the OpenAI Playground: Start by selecting 'Assistants' from the dropdown menu and click on '+ Create assistant.'
  2. Fill out the Details: Enter the name 'Terrific Travels' and provide instructions like 'You are a travel agent specializing in world travel. Your job is to suggest itineraries and give travel tips based on provided data.' Choose the model gpt-4-1106-preview to enable data uploads.

Choose Your Tools

  1. Functions: Use custom functions or external APIs to enhance the assistant's capabilities.
  2. Code Interpreter: Write and execute Python code within a sandboxed environment for dynamic data processing.
  3. Retrieval: Upload files, such as a CSV containing travel preferences, to tailor the assistant's responses.

Save and Run the Assistant

  1. Save the Assistant: After setting up, click 'Save' to store your configuration.
  2. Run the Assistant: Input a message and click 'Run' to receive customized travel recommendations based on the uploaded data.

Creating an Assistant Using the Python APIs

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:

Key Concepts

  • Assistant: Utilizes OpenAI models and tools for intelligent responses.
  • Thread: Represents a conversation session between the user and the assistant.
  • Message: Interactions within the thread, created by either the user or the assistant.
  • Run: Executes the assistant on a thread, orchestrating the models and tools.
  • Run Step: The actions the assistant takes during a run.

Setting Up the Python Code

  1. Install the OpenAI SDK: Use the command pip install --upgrade openai to install the necessary SDK.
  2. Create assistant.py and Add Your CSV File: Prepare your script and data file for integration.

Example Code

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}')

Running the Code

python assistant.py

Execute the script to start the assistant, which will periodically check the run status and print the conversation messages once completed.

Conclusion

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.

Additional Resources

  • Getting Started on Prompt Engineering with Generative AI
  • OpenAI Assistant API course
  • OpenAI: Prompt Engineering Best Practices
  • Developing Generative AI Applications with Python and OpenAI
  • Using ChatGPT to Code a Full-stack Web Application

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.

FAQs

  • What is the OpenAI Assistants API? The OpenAI Assistants API allows users to create customized AI assistants that can integrate data, run code, and use external APIs.
  • How do I start creating an assistant? You can start by using the OpenAI Playground UI or the Python APIs, depending on your comfort with coding.
  • Can I use my own data with these assistants? Yes, you can upload your own data files to customize the assistant's responses.
  • What programming knowledge do I need? Basic knowledge of Python is helpful if you choose to use the Python APIs.
  • Are there resources to learn more? Yes, OpenAI provides courses and best practices for developing AI applications.

Get started with raia today

Sign up to learn more about how raia can help
your business automate tasks that cost you time and money.