Skip to content

Commit

Permalink
ruff ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
rgbkrk committed Feb 27, 2024
1 parent c13f337 commit 5aecc06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
20 changes: 11 additions & 9 deletions chatlab/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ async def __call__(self, *messages: Union[ChatCompletionMessageParam, str], stre
"""Send messages to the chat model and display the response."""
return await self.submit(*messages, stream=stream, **kwargs)

async def __process_stream(self, resp: AsyncStream[ChatCompletionChunk]) -> Tuple[str, Optional[ToolArguments], List[ToolArguments]]:
async def __process_stream(
self, resp: AsyncStream[ChatCompletionChunk]
) -> Tuple[str, Optional[ToolArguments], List[ToolArguments]]:
assistant_view: AssistantMessageView = AssistantMessageView()
function_view: Optional[ToolArguments] = None
finish_reason = None
Expand Down Expand Up @@ -207,15 +209,17 @@ async def __process_stream(self, resp: AsyncStream[ChatCompletionChunk]) -> Tupl

return (finish_reason, function_view, tool_calls)

async def __process_full_completion(self, resp: ChatCompletion) -> Tuple[str, Optional[ToolArguments], List[ToolArguments]]:
async def __process_full_completion(
self, resp: ChatCompletion
) -> Tuple[str, Optional[ToolArguments], List[ToolArguments]]:
assistant_view: AssistantMessageView = AssistantMessageView()
function_view: Optional[ToolArguments] = None

tool_calls: list[ToolArguments] = []

if len(resp.choices) == 0:
logger.warning(f"Result has no choices: {resp}")
return ("stop", None, tool_calls ) # TODO
return ("stop", None, tool_calls) # TODO

choice = resp.choices[0]

Expand Down Expand Up @@ -297,11 +301,9 @@ async def submit(self, *messages: Union[ChatCompletionMessageParam, str], stream

self.append(*messages)

(
finish_reason,
function_call_request,
tool_arguments
) = await self.__process_full_completion(full_response)
(finish_reason, function_call_request, tool_arguments) = await self.__process_full_completion(
full_response
)

except openai.RateLimitError as e:
logger.error(f"Rate limited: {e}. Waiting 5 seconds and trying again.")
Expand All @@ -326,7 +328,7 @@ async def submit(self, *messages: Union[ChatCompletionMessageParam, str], stream
# Reply back to the LLM with the result of the function call, allow it to continue
await self.submit(stream=stream, **kwargs)
return

if finish_reason == "tool_calls":
for tool_argument in tool_arguments:
# Oh crap I need to append the big assistant call of it too. May have to assume we've done it by here.
Expand Down
15 changes: 3 additions & 12 deletions chatlab/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

from typing import Literal, Optional, Required, TypedDict
from typing import Optional

from openai.types.chat import ChatCompletionMessageParam, ChatCompletionToolMessageParam

Expand Down Expand Up @@ -99,22 +99,12 @@ def function_result(name: str, content: str) -> ChatCompletionMessageParam:
"name": name,
}

class ChatCompletionToolMessageParamWithName(TypedDict, total=False):
content: Required[str]
"""The contents of the tool message."""

role: Required[Literal["tool"]]
"""The role of the messages author, in this case `tool`."""

tool_call_id: Required[str]
"""Tool call that this message is responding to."""

class ChatCompletionToolMessageParamWithName(ChatCompletionToolMessageParam):
name: Optional[str]
"""The name of the tool."""




def tool_result(tool_call_id: str, content: str, name: str) -> ChatCompletionToolMessageParamWithName:
"""Create a tool result message.
Expand All @@ -132,6 +122,7 @@ def tool_result(tool_call_id: str, content: str, name: str) -> ChatCompletionToo
"tool_call_id": tool_call_id,
}


# Aliases
narrate = system
human = user
Expand Down

0 comments on commit 5aecc06

Please sign in to comment.