THIS WORKS

This commit is contained in:
James Hush
2025-05-08 14:53:37 +08:00
parent b17165b7ea
commit e09028aca2

View File

@@ -26,6 +26,7 @@ from pipecat.frames.frames import (
LLMMessagesFrame, LLMMessagesFrame,
LLMTextFrame, LLMTextFrame,
LLMUpdateSettingsFrame, LLMUpdateSettingsFrame,
VisionImageRawFrame,
) )
from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.runner import PipelineRunner from pipecat.pipeline.runner import PipelineRunner
@@ -95,6 +96,21 @@ class CustomLLMService(BaseOpenAILLMService):
# tools=[get_weather], # tools=[get_weather],
) )
def create_client(
self,
api_key=None,
base_url=None,
organization=None,
project=None,
default_headers=None,
**kwargs,
):
return Agent(
name="Assistant agent",
instructions="Respond with haikus.",
# tools=[get_weather],
)
def create_context_aggregator( def create_context_aggregator(
self, self,
context: OpenAILLMContext, context: OpenAILLMContext,
@@ -124,31 +140,21 @@ class CustomLLMService(BaseOpenAILLMService):
assistant = OpenAIAssistantContextAggregator(context, params=assistant_params) assistant = OpenAIAssistantContextAggregator(context, params=assistant_params)
return OpenAIContextAggregatorPair(_user=user, _assistant=assistant) return OpenAIContextAggregatorPair(_user=user, _assistant=assistant)
def create_client( async def _process_context(self, context: OpenAILLMContext):
self, functions_list = []
api_key=None, arguments_list = []
base_url=None, tool_id_list = []
organization=None, func_idx = 0
project=None, function_name = ""
default_headers=None, arguments = ""
**kwargs, tool_call_id = ""
):
return Agent(
name="Assistant agent",
instructions="Respond with haikus.",
# tools=[get_weather],
)
async def get_chat_completions( await self.start_ttfb_metrics()
self, context: OpenAILLMContext, messages: List[ChatCompletionMessageParam]
) -> AsyncStream[ChatCompletionChunk]:
# self._client.tools = context.tools
logger.info(f"get_chat_completions: {self._client}")
result = Runner.run_streamed( result = Runner.run_streamed(
# context=context, # context=context,
starting_agent=self._client, starting_agent=self._client,
input="Tell a joke about pirates.", # messages input=context.messages, # messages
# --- # ---
# no func tool # no func tool
# input="give me a 2 sentences about life", # input="give me a 2 sentences about life",
@@ -161,23 +167,31 @@ class CustomLLMService(BaseOpenAILLMService):
return return
async for event in result.stream_events(): async for event in result.stream_events():
# if not event.type == "raw_response_event":
# break
if event.type == "raw_response_event": if event.type == "raw_response_event":
if event.data.type == "response.output_text.delta": if event.data.type == "response.output_text.delta":
delta = ChoiceDelta(content=event.data.delta) await self.push_frame(LLMTextFrame(event.data.delta))
choice = Choice(delta=delta, index=event.data.output_index)
converted_message = ChatCompletionChunk( async def process_frame(self, frame: Frame, direction: FrameDirection):
id=event.data.item_id, await super().process_frame(frame, direction)
choices=[choice],
)
return converted_message
else:
break
# chunks = await self._client.chat.completions.create(**params) context = None
# return chunks if isinstance(frame, OpenAILLMContextFrame):
context: OpenAILLMContext = frame.context
elif isinstance(frame, LLMMessagesFrame):
context = OpenAILLMContext.from_messages(frame.messages)
else:
await self.push_frame(frame, direction)
if context:
try:
await self.push_frame(LLMFullResponseStartFrame())
await self.start_processing_metrics()
await self._process_context(context)
except httpx.TimeoutException:
await self._call_event_handler("on_completion_timeout")
finally:
await self.stop_processing_metrics()
await self.push_frame(LLMFullResponseEndFrame())
async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace): async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace):