Skip to content

Commit 6579109

Browse files
Set up strongly typed create_assistant script
1 parent c225a7c commit 6579109

File tree

4 files changed

+73
-85
lines changed

4 files changed

+73
-85
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@ uv venv
2626
uv pip install -r pyproject.toml
2727
```
2828

29-
### 4. Run
29+
### 4. Create an assistant
30+
31+
```shell
32+
uv run create_assistant.py
33+
```
34+
35+
### 5. Run the FastAPI server
3036

3137
```shell
3238
uv run uvicorn main:app --host 0.0.0.0 --port 8000 --reload
3339
```
3440

35-
### 5. Navigate to [http://localhost:3000](http://localhost:3000).
41+
### 6. Navigate to [http://localhost:3000](http://localhost:3000).
3642

3743
## Overview
3844

create_assistant.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import asyncio
2+
from dotenv import load_dotenv
3+
from openai import AsyncOpenAI
4+
from openai.types.beta.assistant_create_params import AssistantCreateParams
5+
from openai.types.beta.assistant_tool_param import CodeInterpreterToolParam, FileSearchToolParam, FunctionToolParam
6+
from openai.types.beta.assistant import Assistant
7+
from openai.types import FunctionDefinition
8+
9+
request: AssistantCreateParams = AssistantCreateParams(
10+
instructions="You are a helpful assistant.",
11+
name="Quickstart Assistant",
12+
model="gpt-4o",
13+
tools=[
14+
CodeInterpreterToolParam(type="code_interpreter"),
15+
FileSearchToolParam(type="file_search"),
16+
FunctionToolParam(
17+
type="function",
18+
function=FunctionDefinition(
19+
name="get_weather",
20+
description="Determine weather in my location",
21+
parameters={
22+
"type": "object",
23+
"properties": {
24+
"location": {
25+
"type": "string",
26+
"description": "The city and state e.g. San Francisco, CA"
27+
}
28+
},
29+
"required": ["location"],
30+
"additionalProperties": False,
31+
},
32+
strict=True,
33+
)
34+
),
35+
],
36+
)
37+
38+
39+
async def main():
40+
# Create a new assistant using the OpenAI client
41+
assistant: Assistant = await openai.beta.assistants.create(**request)
42+
return assistant
43+
44+
45+
# Run the assistant creation in an asyncio event loop
46+
if __name__ == "__main__":
47+
import logging
48+
import sys
49+
50+
# Initialize the OpenAI client
51+
load_dotenv()
52+
openai: AsyncOpenAI = AsyncOpenAI()
53+
54+
# Configure logger to stream to console
55+
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
56+
logger: logging.Logger = logging.getLogger(__name__)
57+
58+
# Run the main function in an asyncio event loop
59+
assistant: Assistant = asyncio.run(main())
60+
logger.info(f"Assistant created with ID: {assistant.id}")

routes/assistants.py

Lines changed: 0 additions & 81 deletions
This file was deleted.

routes/assistants_files.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
from dotenv import load_dotenv
12
from fastapi import FastAPI, Request, UploadFile, File
23
from pydantic import BaseModel
34
from openai import AsyncOpenAI
45

6+
load_dotenv()
7+
58
app = FastAPI()
69

710
# Initialize OpenAI client
8-
openai = AsyncOpenAI(api_key="your-api-key")
11+
openai = AsyncOpenAI()
912

1013
# Pydantic model for DELETE request body
1114
class DeleteRequest(BaseModel):
1215
fileId: str
1316

1417
# Helper function to get or create a vector store
15-
async def get_or_create_vector_store() -> str:
18+
async def get_or_create_vector_store(assistantId: str) -> str:
1619
assistant = await openai.beta.assistants.retrieve(assistantId)
1720
if assistant.tool_resources and assistant.tool_resources.file_search and assistant.tool_resources.file_search.vector_store_ids:
1821
return assistant.tool_resources.file_search.vector_store_ids[0]

0 commit comments

Comments
 (0)