-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Splits up router into Runs, Messages, Threads, and Run-Steps * Implemented Runs endpoints * Adds schema for Runs * Adds Tests for Runs, Messages, and Threads * Fixes a few issues from other parts of the code to ensure Runs works correctly * Remove old RAG deployment from UDS Bundles --------- Co-authored-by: Gregory Horvath <gphorvath@defenseunicorns.com>
- Loading branch information
1 parent
c3d4883
commit fecf0f8
Showing
44 changed files
with
2,353 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
-- Create a table to store the OpenAI Run Objects | ||
create table | ||
run_objects ( | ||
id uuid primary key default uuid_generate_v4(), | ||
user_id uuid references auth.users not null, | ||
object text check (object in ('thread.run')), | ||
created_at bigint default extract(epoch FROM NOW()) not null, | ||
thread_id uuid references thread_objects (id) on delete cascade not null, | ||
assistant_id uuid references assistant_objects (id) on delete cascade not null, | ||
status text, | ||
required_action jsonb, | ||
last_error jsonb, | ||
expires_at bigint, | ||
started_at bigint, | ||
cancelled_at bigint, | ||
failed_at bigint, | ||
completed_at bigint, | ||
model text, | ||
instructions text, | ||
tools jsonb, | ||
metadata jsonb, | ||
parallel_tool_calls boolean, | ||
stream boolean, | ||
file_ids uuid[], | ||
incomplete_details jsonb, | ||
usage jsonb, | ||
temperature float, | ||
top_p float, | ||
max_prompt_tokens int, | ||
max_completion_tokens int, | ||
truncation_strategy jsonb, | ||
tool_choice jsonb, | ||
response_format jsonb | ||
); | ||
|
||
-- RLS policies | ||
alter table run_objects enable row level security; | ||
|
||
-- Policies for run_objects | ||
create policy "Individuals can view their own run_objects." on run_objects for | ||
select using (auth.uid() = user_id); | ||
create policy "Individuals can create run_objects." on run_objects for | ||
insert with check (auth.uid() = user_id); | ||
create policy "Individuals can update their own run_objects." on run_objects for | ||
update using (auth.uid() = user_id); | ||
create policy "Individuals can delete their own run_objects." on run_objects for | ||
delete using (auth.uid() = user_id); | ||
|
||
-- Indexes for common filtering and sorting for run_objects | ||
CREATE INDEX run_objects_id ON run_objects (id); | ||
CREATE INDEX run_objects_user_id ON run_objects (user_id); | ||
CREATE INDEX run_objects_created_at ON run_objects (created_at); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"""Converters for the LeapfrogAI API""" | ||
|
||
from typing import Iterable | ||
from openai.types.beta import AssistantStreamEvent | ||
from openai.types.beta.assistant_stream_event import ThreadMessageDelta | ||
from openai.types.beta.threads.file_citation_annotation import FileCitation | ||
from openai.types.beta.threads import ( | ||
MessageContentPartParam, | ||
MessageContent, | ||
TextContentBlock, | ||
Text, | ||
Message, | ||
MessageDeltaEvent, | ||
MessageDelta, | ||
TextDeltaBlock, | ||
TextDelta, | ||
FileCitationAnnotation, | ||
) | ||
|
||
|
||
def from_assistant_stream_event_to_str(stream_event: AssistantStreamEvent): | ||
return f"event: {stream_event.event}\ndata: {stream_event.data.model_dump_json()}" | ||
|
||
|
||
def from_content_param_to_content( | ||
thread_message_content: str | Iterable[MessageContentPartParam], | ||
) -> MessageContent: | ||
"""Converts messages from MessageContentPartParam to MessageContent""" | ||
if isinstance(thread_message_content, str): | ||
return TextContentBlock( | ||
text=Text(annotations=[], value=thread_message_content), | ||
type="text", | ||
) | ||
else: | ||
result: str = "" | ||
|
||
for message_content_part in thread_message_content: | ||
if isinstance(text := message_content_part.get("text"), str): | ||
result += text | ||
|
||
return TextContentBlock( | ||
text=Text(annotations=[], value=result), | ||
type="text", | ||
) | ||
|
||
|
||
def from_text_to_message(text: str, file_ids: list[str]) -> Message: | ||
all_file_ids: str = "" | ||
|
||
for file_id in file_ids: | ||
all_file_ids += f" [{file_id}]" | ||
|
||
message_content: TextContentBlock = TextContentBlock( | ||
text=Text( | ||
annotations=[ | ||
FileCitationAnnotation( | ||
text=f"[{file_id}]", | ||
file_citation=FileCitation(file_id=file_id, quote=""), | ||
start_index=0, | ||
end_index=0, | ||
type="file_citation", | ||
) | ||
for file_id in file_ids | ||
], | ||
value=text + all_file_ids, | ||
), | ||
type="text", | ||
) | ||
|
||
new_message = Message( | ||
id="", | ||
created_at=0, | ||
object="thread.message", | ||
status="in_progress", | ||
thread_id="", | ||
content=[message_content], | ||
role="assistant", | ||
metadata=None, | ||
) | ||
|
||
return new_message | ||
|
||
|
||
async def from_chat_completion_choice_to_thread_message_delta( | ||
index, random_uuid, streaming_response | ||
) -> ThreadMessageDelta: | ||
thread_message_event: ThreadMessageDelta = ThreadMessageDelta( | ||
data=MessageDeltaEvent( | ||
id=str(random_uuid), | ||
delta=MessageDelta( | ||
content=[ | ||
TextDeltaBlock( | ||
index=index, | ||
type="text", | ||
text=TextDelta( | ||
annotations=[], | ||
value=streaming_response.choices[0].chat_item.content, | ||
), | ||
) | ||
], | ||
role="assistant", | ||
), | ||
object="thread.message.delta", | ||
), | ||
event="thread.message.delta", | ||
) | ||
return thread_message_event |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.