Skip to content

Commit 2d9a06b

Browse files
Update dependencies and lint types
1 parent 3d2ce97 commit 2d9a06b

File tree

6 files changed

+145
-120
lines changed

6 files changed

+145
-120
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ __pycache__
77
.specstory
88
.mypy_cache
99
.cursorrules
10-
.repomix-output.txt
10+
.repomix-output.txt
11+
repomix-output.txt
12+
artifacts/

routers/chat.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
from fastapi import APIRouter, Form, Depends, Request
88
from fastapi.responses import StreamingResponse, HTMLResponse
99
from openai import AsyncOpenAI
10-
from openai.resources.beta.threads.runs.runs import AsyncAssistantStreamManager
10+
from openai.lib.streaming._assistants import AsyncAssistantStreamManager, AsyncAssistantEventHandler
1111
from openai.types.beta.assistant_stream_event import (
1212
ThreadMessageCreated, ThreadMessageDelta, ThreadRunCompleted,
1313
ThreadRunRequiresAction, ThreadRunStepCreated, ThreadRunStepDelta
1414
)
1515
from openai.types.beta import AssistantStreamEvent
16-
from openai.lib.streaming._assistants import AsyncAssistantEventHandler
1716
from openai.types.beta.threads.run_submit_tool_outputs_params import ToolOutput
18-
from openai.types.beta.threads.run import RequiredAction, Run
17+
from openai.types.beta.threads.run import RequiredAction
18+
from openai.types.beta.threads.message_content_delta import MessageContentDelta
19+
from openai.types.beta.threads.text_delta_block import TextDeltaBlock
1920
from fastapi.responses import StreamingResponse
2021
from fastapi import APIRouter, Depends, Form, HTTPException
2122
from pydantic import BaseModel
@@ -188,8 +189,8 @@ async def handle_assistant_stream(
188189
time.sleep(0.25) # Give the client time to render the message
189190

190191
if isinstance(event, ThreadMessageDelta) and event.data.delta.content:
191-
content = event.data.delta.content[0]
192-
if hasattr(content, 'text') and content.text and content.text.value:
192+
content: MessageContentDelta = event.data.delta.content[0]
193+
if isinstance(content, TextDeltaBlock) and content.text and content.text.value:
193194
yield sse_format(
194195
f"textDelta{step_id}",
195196
content.text.value
@@ -297,8 +298,8 @@ async def event_generator() -> AsyncGenerator[str, None]:
297298
location = args.get("location", "Unknown")
298299
dates_raw = args.get("dates", [datetime.today().strftime("%Y-%m-%d")])
299300
dates = [
300-
datetime.strptime(d, "%Y-%m-%d") if isinstance(d, str) else d
301-
for d in dates_raw
301+
datetime.strptime(d, "%Y-%m-%d")
302+
for d in dates_raw if isinstance(d, str)
302303
]
303304
except Exception as err:
304305
logger.error(f"Failed to parse function arguments: {err}")

routers/files.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def get_or_create_vector_store(assistantId: str, client: AsyncOpenAI = Dep
2525
assistant = await client.beta.assistants.retrieve(assistantId)
2626
if assistant.tool_resources and assistant.tool_resources.file_search and assistant.tool_resources.file_search.vector_store_ids:
2727
return assistant.tool_resources.file_search.vector_store_ids[0]
28-
vector_store = await client.beta.vector_stores.create(name="sample-assistant-vector-store")
28+
vector_store = await client.vector_stores.create(name="sample-assistant-vector-store")
2929
await client.beta.assistants.update(
3030
assistantId,
3131
tool_resources={
@@ -40,13 +40,13 @@ async def get_or_create_vector_store(assistantId: str, client: AsyncOpenAI = Dep
4040
async def list_files(client: AsyncOpenAI = Depends(lambda: AsyncOpenAI())) -> List[Dict[str, str]]:
4141
# List files in the vector store
4242
vector_store_id = await get_or_create_vector_store(assistant_id, client)
43-
file_list = await client.beta.vector_stores.files.list(vector_store_id)
43+
file_list = await client.vector_stores.files.list(vector_store_id)
4444
files_array: List[Dict[str, str]] = []
4545

4646
if file_list.data:
4747
for file in file_list.data:
4848
file_details = await client.files.retrieve(file.id)
49-
vector_file_details = await client.beta.vector_stores.files.retrieve(
49+
vector_file_details = await client.vector_stores.files.retrieve(
5050
vector_store_id=vector_store_id,
5151
file_id=file.id
5252
)
@@ -66,7 +66,7 @@ async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]:
6666
file=file.file,
6767
purpose="assistants"
6868
)
69-
await client.beta.vector_stores.files.create(
69+
await client.vector_stores.files.create(
7070
vector_store_id=vector_store_id,
7171
file_id=openai_file.id
7272
)
@@ -103,7 +103,7 @@ async def delete_file(
103103
client: AsyncOpenAI = Depends(lambda: AsyncOpenAI())
104104
) -> Dict[str, str]:
105105
vector_store_id = await get_or_create_vector_store(assistant_id, client)
106-
await client.beta.vector_stores.files.delete(vector_store_id=vector_store_id, file_id=fileId)
106+
await client.vector_stores.files.delete(vector_store_id=vector_store_id, file_id=fileId)
107107
return {"message": "File deleted successfully"}
108108

109109
@router.get("/{file_id}/content")

utils/create_assistant.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from openai import AsyncOpenAI
77
from openai.types.beta.assistant_create_params import AssistantCreateParams
88
from openai.types.beta.assistant_update_params import AssistantUpdateParams
9-
from openai.types.beta.assistant_tool_param import CodeInterpreterToolParam, FileSearchToolParam, FunctionToolParam
9+
from openai.types.beta.code_interpreter_tool_param import CodeInterpreterToolParam
10+
from openai.types.beta.file_search_tool_param import FileSearchToolParam
11+
from openai.types.beta.function_tool_param import FunctionToolParam
1012
from openai.types.beta.assistant import Assistant
1113
from openai.types.shared_params.function_definition import FunctionDefinition
1214
from openai.types.beta.file_search_tool_param import FileSearch
@@ -93,12 +95,12 @@ async def create_or_update_assistant(
9395
assistant_id: str | None,
9496
request: AssistantCreateParams | AssistantUpdateParams,
9597
logger: logging.Logger
96-
) -> str:
98+
) -> str | None:
9799
"""
98100
Create or update the assistant based on the presence of an assistant_id.
99101
"""
102+
assistant = None # Initialize assistant
100103
try:
101-
assistant: Assistant
102104
if assistant_id:
103105
# Update the existing assistant
104106
assistant = await client.beta.assistants.update(
@@ -120,7 +122,7 @@ async def create_or_update_assistant(
120122
action = "update" if assistant_id else "create"
121123
logger.error(f"Failed to {action} assistant: {e}")
122124

123-
return assistant.id
125+
return assistant.id if assistant else None # Conditionally return ID
124126

125127

126128
# Run the assistant creation in an asyncio event loop

utils/custom_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import random
22
import logging
33
from datetime import datetime
4+
from typing import Sequence
45

56
logger = logging.getLogger("uvicorn.error")
67

7-
def get_weather(location, dates: list[str | datetime] = [datetime.today()]):
8+
def get_weather(location, dates: Sequence[str | datetime] = [datetime.today()]):
89
"""
910
Generate random weather reports for a given location over a date range.
1011
@@ -36,4 +37,3 @@ def get_weather(location, dates: list[str | datetime] = [datetime.today()]):
3637
})
3738

3839
return weather_reports
39-

0 commit comments

Comments
 (0)