Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
142 views45 pages

OpenAI Assistant Slides

Uploaded by

sewwaaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views45 pages

OpenAI Assistant Slides

Uploaded by

sewwaaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

How to Build

AI Assistants
ZJ
How to Build
AI Assistants
Course Curriculum
ZJ
Course Curriculum
• OpenAI Assistants Playground
• What is Assistants
• Assistants, Threads, Messages & Runs
• OpenAI Assistants Tools:
- Code interpreter
- Knowledge retrieval
ZJ
- Function calling How to Build AI Assistants
OpenAI Assistants Playground
• You will get a good feel of the various
elements of Open AI’s Assistants
• Understand capabilities of Assistants
• Harness the power of OpenAI Python API
to build intelligent Assistants
• Use various tools and models to respond
ZJ to your queries
How to Build AI Assistants
Assistants, Threads, Messages & Runs
• Core of Assistants API - get a deeper
understanding of the Components
• Understand the typical Assistants API
integration low

ZJ
How to Build AI Assistants
f
OpenAI Assistants API - Tools
• Currently supports three types:
- Code interpreter
- Knowledge retrieval
- Function calling

ZJ
How to Build AI Assistants
OpenAI Assistants API - Code Interpreter
• You will use Code interpreter to process
queries and iles with your data
• Dive deep into processing diverse data
formats and creating iles with dynamic
content, including captivating graphs and
images.

ZJ
How to Build AI Assistants
f
f
OpenAI Assistants API - Knowledge retrieval
• Augment your Assistant’s knowledge
• Pass iles such as PDFs, code ile,
powerpoint, images, word documents
• Then use these iles as context in your
conversation with your Assistant

ZJ
How to Build AI Assistants
f
f
f
OpenAI Assistants API - Function calling
• You will use your custom functions with
with Assistants
• Function calling makes OpenAI more
powerful

ZJ
How to Build AI Assistants
How to Build
AI Assistants
What is Assistants
ZJ
What is Assistants
• Assistants API:
- Assistants - encapsulate a base model,
instructions, tools and context, documents
- Stateful with Threads - state of a
conversation
- Runs - power the execution of an Assistant
on a Thread including textual responses and
ZJ
multi-step tool use How to Build AI Assistants
Tools Model
User
Run
Message
Message
Assistant Message
Thread
File Message

Tools File Model

Code interpreter
Knowledge retrieval
Function call

ZJ
How to Build AI Assistants
What is Assistants
• Chat Completions API:
- Messages on which we perform
Completions with a Model (gpt-4, etc)
- Stateless - Need to manage conversation
state, tools de inition, retrieval
documents and code execution manually

ZJ
How to Build AI Assistants
f
Conclusion
• Assistants API is more suitable for those
looking for advanced context management
• And implementation of complex tools like
RAGs
• Chat Completions API if you need simplicity,
speed, and high customization
• However, greater challenge with complex
ZJ
context management. How to Build AI Assistants
How to Build
AI Assistants
Assistant Setup
ZJ
Pre-requisites for Assistant Playground
• OpenAI API account with a payment method
- https://openai.com/

ZJ
How to Build AI Assistants
Run Steps
Run Steps
Run Steps

Run Steps Run Steps


Run Run Steps Run Steps User
Run Steps Run Steps
Message Message
Message Assistant Message
Thread
Message File Message

Tools File Model

Code interpreter
Knowledge retrieval
Function call

ZJ
How to Build AI Assistants
Pre-requisites for this course
• Python installed on your computer
- https://www.python.org/downloads/
- https://www.anaconda.com/download
• OpenAI API account with a payment method
- https://openai.com/
• Jupyter notebook
ZJ
How to Build AI Assistants
Getting Started
• Install or upgrade the python OpenAI api library
pip install --upgrade openai
• Get an OpenAI account and set-up a payment method
• Get an OpenAI API key
-
https://platform.openai.com/api-keys
• Store your API key
-
Create .env ile in your project folder
-
Or Windows Environment Variables
ZJ -
Or Mac bash pro ile /.bash_pro ile or /.zshrc add
How to Build AI Assistants
f
f
f
Setup your API Key on WINDOWS

• To make the setup permanent, add the variable through the system properties as
follows:

A. Right-click on 'This PC' or 'My Computer' and select 'Properties'.

B. Click on 'Advanced system settings'.

C. Click the 'Environment Variables' button.

D. In the 'System variables' section, click 'New...' and enter OPENAI_API_KEY as


the variable name and your API key as the variable value.

• Veri ication: To verify the setup, reopen the command prompt and type the command
below. It should display your API key: echo %OPENAI_API_KEY%

ZJ
How to Build AI Assistants
f
Setup your API Key on MAC

• Open Terminal: You can ind it in the Applications folder or search for it using
Spotlight (Command + Space).

• Edit Bash Pro ile: Use the command nano ~/.bash_pro ile or nano ~/.zshrc (for newer
MacOS versions) to open the pro ile ile in a text editor.

• Add Environment Variable: In the editor, add the line below, replacing your-api-key-
here with your actual API key:

export OPENAI_API_KEY='your-api-key-here'

• Save and Exit: Press Ctrl+O to write the changes, followed by Ctrl+X to close the
editor.

• Load Your Pro ile: Use the command source ~/.bash_pro ile or source ~/.zshrc to load
the updated pro ile.

• Veri ication: Verify the setup by typing echo $OPENAI_API_KEY in the terminal. It
ZJ should display your API key.
How to Build AI Assistants
f
f
f
f
f
f
f
f
f
How to Build
AI Assistants
Assistant Tools
ZJ
OpenAI Assistant
• You can build AI Assistants within your apps
• Your purpose built Assistant with Assistants API:
- Uses OpenAI models and can use your iles
- Give speci ic instructions to tune personality
and capabilities
- Can access persistent Threads.
- Use multiple Tools in parallel: Code Interpreter,
ZJ Retrieval and Function call
How to Build AI Assistants
f
f
Assistants Tools
• Code Interpreter
- Bridge gap between human language and
computer code
- Understand complex problems and generates
human like responses
- Uses reinforcement learning
- Better predictions over time

ZJ
How to Build AI Assistants
Assistants Tools
• Code Interpreter
- Run Python in a sandboxed environment
- Can solve challenging code and math problem
- Can analyse your data iles
- Can generate graphs and data iles
- It will keep iterating on the code till it solves the
problem
ZJ - You are charged by the hour per session
How to Build AI Assistants
f
f
Assistants Tools
• Knowledge Retrieval
- Uses your documents and other data you
provide to augment OpenAI’s models
- OpenAPI will automatically chunk, index and
store the embeddings
- Automatically implements vector search to
retrieve content from long documents to
answer your queries
ZJ
How to Build AI Assistants
Assistants Tools
• Function calling
- Function calling allows you to more reliably get
structured data back from the model
- Can call multiple functions in parallel
- Use your functions:
- Create assistants that answer questions by
calling external APIs
- Convert natural language into API calls
ZJ - Extract structured data from text How to Build AI Assistants
Tools Model
User
Run
Message
Message
Assistant Message
Thread
File Message

Tools File Model

Code interpreter
Knowledge retrieval
Function call

ZJ
How to Build AI Assistants
How to Build
AI Assistants
Threads & Messages
ZJ
Tools Model
Run Steps
Run Run Steps
Run Steps
User
Message
Message
Message
Message
Assistant Message
Thread Message

Tools File Model

Code interpreter
Knowledge retrieval
Function call
ZJ
How to Build AI Assistants
Assistant

Message 6

Message 5

Thread Message 4

Message 3

Message 2

Message 1

User
ZJ
How to Build AI Assistants
Assistant Assistant

Message 6

Message 5

Thread Message 4

Message 3

Message 2

Message 1

User
ZJ
How to Build AI Assistants
Threads & Messages
• Threads simplify AI application development
• A Thread is a conversation session between an
Assistant and a user.
• Store message history and truncating it when the
conversation gets too long for the model’s context
length
• Persistent - hence do not need re-send the entire
message history each time - Note charged for the
ZJ tokens for the entire conversation history for each Run
How to Build AI Assistants
Threads
• By maintaining persistent long threads and
thread state management- no need for you to
manage your conversation history
• Create a Thread once, and simply append
Messages to it as your Assistants reply
• No limit on number of messages on a Thread.

ZJ
How to Build AI Assistants
Threads Locks
• When a Run is in_progress and not in a
terminal state, the Thread is locked:
- New Messages cannot be added to the
Thread
- New Runs cannot be created on the Thread

ZJ
How to Build AI Assistants
Messages
• Created by Assistant or User (roles)
• Includes text, images or other iles (context)
• Include annotation - two types of Annotations:
- ile_citation: Created by retrieval tool and de ine
references to a speci ic quote in a speci ic ile
used Assistant to generate the response
- ile_path: Created by code_interpreter tool and
references iles generated by the tool
ZJ • Stored as a reverse list on the Thread How to Build AI Assistants
f
f
f
f
f
f
f
f
How to Build
AI Assistants
Runs
ZJ
Tools Model
Run Steps
Run Run Steps
Run Steps
User
Message
Message
Message
Message
Assistant Message
Thread Message

Tools File Model

Code interpreter
Knowledge retrieval
Function call
ZJ
How to Build AI Assistants
Runs
• When Thread is ready with context and latest message
• Run Thread with Assistant of your choice
- Example: Retail customer scenario
- Run thread with a returns Assistant
- Run the same thread with an information Assistant
• By default, Run will use the model and tools con iguration
speci ied in your Assistant object
• Can override Model, Instruction and Tools when creating the
Run for added lexibility (not ile_ids)
ZJ
• Check Run Step object step_details ield How to Build AI Assistants
f
f
f
f
f
Runs
• Run is an asynchronous operation
• Returns the Run metadata immediately
• Status immediately queued
• Status updated as Assistant performs
operation

ZJ
How to Build AI Assistants
Run Step
• Run Steps created when Assistant:
- Creates a message on a Thread
- Calls a tool
• Steps Assistant took as part of a Run
• Allows you to inspect how Assistant
reached its inal answer

ZJ
How to Build AI Assistants
f
name = ‘<a name>’,
instructions = ‘<Assistant behaviour and response>’,
Assistant tools=[dictionary of tools],
models=‘<model to be used>’
ile_ids = [ ile.id]
Run Steps File
Run Thread
Run Steps

Message 6
thread_id = thread.id,
assistant_id = assistant.id Message 5

Message 4

Message 3

Message 2
thread_id - thread.id,
Message 1 role = ‘user’,
content = ‘ <your message>’

ZJ User How to Build AI Assistants


f
f
How to Build
AI Assistants
Assistants API with
ZJ
Function Calling
Assistants Tools - Function calling
- Function calling allows you to more reliably get
structured data back from the model
- Can call multiple functions in parallel
- Use your functions:
• Create assistants that answer questions by
calling external APIs
• Convert natural language into API calls
• Extract structured data from text
ZJ
How to Build AI Assistants
Assistants Tools - Function calling
• In this lesson:
• Create a function call that calls a news API
• Assistant will query about current news
• Assistant will call the function to get current
news
• Assistant will summarise the news

ZJ
How to Build AI Assistants

You might also like