DEV Community

parmarjatin4911@gmail.com
parmarjatin4911@gmail.com

Posted on

AI Assistants API + Knowledge Retrieval

**
AI
Assistants API + Knowledge Retrieval**

import openai
import time

Initialize the client

client = openai.OpenAI()

file = client.files.create(
file=open("songs.txt", "rb"),
purpose='assistants'
)

Step 1: Create an Assistant

assistant = client.beta.assistants.create(
name="Customer Service Assistant",
instructions="You are a customer support chatbot. Use your knowledge base to best respond to customer queries.",
model="gpt-4-1106-preview",
tools=[{"type": "retrieval"}],
file_ids=[file.id]
)

Step 2: Create a Thread

thread = client.beta.threads.create()

Step 3: Add a Message to a Thread

message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="Tell me about Dance Monkey"
)

Step 4: Run the Assistant

run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Mervin Praison"
)

print(run.model_dump_json(indent=4))

while True:
# Wait for 5 seconds
time.sleep(5)

# Retrieve the run status
run_status = client.beta.threads.runs.retrieve(
    thread_id=thread.id,
    run_id=run.id
)
print(run_status.model_dump_json(indent=4))

# If run is completed, get messages
if run_status.status == 'completed':
    messages = client.beta.threads.messages.list(
        thread_id=thread.id
    )

    # Loop through messages and print content based on role
    for msg in messages.data:
        role = msg.role
        content = msg.content[0].text.value
        print(f"{role.capitalize()}: {content}")
    break
else:
    print("Waiting for the Assistant to process...")
    time.sleep(5)
Enter fullscreen mode Exit fullscreen mode

Categories
AI
OpenAPI Weather Schema

Post author
Enter fullscreen mode Exit fullscreen mode

By praison

Post date

November 11, 2023
Enter fullscreen mode Exit fullscreen mode

{
"openapi": "3.1.0",
"info": {
"title": "Get weather data",
"description": "Retrieves current weather data for a location based on wttr.in.",
"version": "v1.0.0"
},
"servers": [
{
"url": "https://wttr.in"
}
],
"paths": {
"/{location}": {
"get": {
"description": "Get weather information for a specific location",
"operationId": "GetCurrentWeather",
"parameters": [
{
"name": "location",
"in": "path",
"description": "City or location to retrieve the weather for",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
},
"404": {
"description": "Location not found"
}
},
"deprecated": false
}
}
},
"components": {
"schemas": {}
}
}

Top comments (0)