Allow for an override of running a completion after a function call completes, OpenAI
This commit is contained in:
@@ -330,6 +330,7 @@ class FunctionCallResultFrame(DataFrame):
|
|||||||
arguments: str
|
arguments: str
|
||||||
result: Any
|
result: Any
|
||||||
run_llm: bool = True
|
run_llm: bool = True
|
||||||
|
override_run_llm: bool = False
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import copy
|
|||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Awaitable, Callable, List
|
from typing import Any, Awaitable, Callable, List, Optional
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
@@ -219,22 +219,31 @@ class OpenAILLMContext:
|
|||||||
|
|
||||||
# Define a callback function that pushes a FunctionCallResultFrame upstream & downstream.
|
# Define a callback function that pushes a FunctionCallResultFrame upstream & downstream.
|
||||||
async def function_call_result_callback(result):
|
async def function_call_result_callback(result):
|
||||||
|
# Extract result and frame parameters if provided
|
||||||
|
if isinstance(result, dict) and "result" in result:
|
||||||
|
frame_run_llm = result.get("run_llm", run_llm)
|
||||||
|
frame_override = result.get("override_run_llm", False)
|
||||||
|
else:
|
||||||
|
frame_run_llm = run_llm
|
||||||
|
frame_override = False
|
||||||
|
|
||||||
result_frame_downstream = FunctionCallResultFrame(
|
result_frame_downstream = FunctionCallResultFrame(
|
||||||
function_name=function_name,
|
function_name=function_name,
|
||||||
tool_call_id=tool_call_id,
|
tool_call_id=tool_call_id,
|
||||||
arguments=arguments,
|
arguments=arguments,
|
||||||
result=result,
|
result=result,
|
||||||
run_llm=run_llm,
|
run_llm=frame_run_llm,
|
||||||
|
override_run_llm=frame_override,
|
||||||
)
|
)
|
||||||
result_frame_upstream = FunctionCallResultFrame(
|
result_frame_upstream = FunctionCallResultFrame(
|
||||||
function_name=function_name,
|
function_name=function_name,
|
||||||
tool_call_id=tool_call_id,
|
tool_call_id=tool_call_id,
|
||||||
arguments=arguments,
|
arguments=arguments,
|
||||||
result=result,
|
result=result,
|
||||||
run_llm=run_llm,
|
run_llm=frame_run_llm,
|
||||||
|
override_run_llm=frame_override,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Push frame both downstream and upstream
|
|
||||||
await llm.push_frame(result_frame_downstream, FrameDirection.DOWNSTREAM)
|
await llm.push_frame(result_frame_downstream, FrameDirection.DOWNSTREAM)
|
||||||
await llm.push_frame(result_frame_upstream, FrameDirection.UPSTREAM)
|
await llm.push_frame(result_frame_upstream, FrameDirection.UPSTREAM)
|
||||||
|
|
||||||
|
|||||||
@@ -580,8 +580,13 @@ class OpenAIAssistantContextAggregator(LLMAssistantContextAggregator):
|
|||||||
"tool_call_id": frame.tool_call_id,
|
"tool_call_id": frame.tool_call_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
# Only run the LLM if there are no more function calls in progress.
|
|
||||||
run_llm = not bool(self._function_calls_in_progress)
|
if frame.override_run_llm:
|
||||||
|
# Explicit override
|
||||||
|
run_llm = frame.run_llm
|
||||||
|
else:
|
||||||
|
# Default behavior
|
||||||
|
run_llm = not bool(self._function_calls_in_progress)
|
||||||
else:
|
else:
|
||||||
self._context.add_message({"role": "assistant", "content": aggregation})
|
self._context.add_message({"role": "assistant", "content": aggregation})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user