formatting
This commit is contained in:
@@ -36,7 +36,7 @@ logger.add(sys.stderr, level="DEBUG")
|
|||||||
|
|
||||||
|
|
||||||
async def main(room_url: str, token):
|
async def main(room_url: str, token):
|
||||||
|
|
||||||
timestamp = int(time.time())
|
timestamp = int(time.time())
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
|||||||
@@ -26,11 +26,21 @@ from pipecat.frames.frames import (
|
|||||||
|
|
||||||
class BaseOpenPipeLLMService(LLMService):
|
class BaseOpenPipeLLMService(LLMService):
|
||||||
|
|
||||||
def __init__(self, model: str, c_id=None, api_key=None, openpipe_api_key=None, openpipe_base_url=None, prompt=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
model: str,
|
||||||
|
c_id=None,
|
||||||
|
api_key=None,
|
||||||
|
openpipe_api_key=None,
|
||||||
|
openpipe_base_url=None,
|
||||||
|
prompt=None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._model = model
|
self._model = model
|
||||||
self._client = self.create_client(api_key=api_key, openpipe_api_key=openpipe_api_key, openpipe_base_url=openpipe_base_url)
|
self._client = self.create_client(
|
||||||
self.c_id=c_id if c_id else secrets.token_urlsafe(16)
|
api_key=api_key,
|
||||||
|
openpipe_api_key=openpipe_api_key,
|
||||||
|
openpipe_base_url=openpipe_base_url)
|
||||||
|
self.c_id = c_id if c_id else secrets.token_urlsafe(16)
|
||||||
self.prompt = prompt
|
self.prompt = prompt
|
||||||
logger.debug(f"Client Created: {self._client}")
|
logger.debug(f"Client Created: {self._client}")
|
||||||
|
|
||||||
@@ -46,41 +56,41 @@ class BaseOpenPipeLLMService(LLMService):
|
|||||||
return client
|
return client
|
||||||
|
|
||||||
async def _stream_chat_completions(self, context):
|
async def _stream_chat_completions(self, context):
|
||||||
logger.debug(f"Generating chat: {context.get_messages_json()}")
|
logger.debug(f"Generating chat: {context.get_messages_json()}")
|
||||||
|
|
||||||
messages: List[ChatCompletionMessageParam] = context.get_messages()
|
messages: List[ChatCompletionMessageParam] = context.get_messages()
|
||||||
|
|
||||||
# base64 encode any images
|
# base64 encode any images
|
||||||
for message in messages:
|
for message in messages:
|
||||||
if message.get("mime_type") == "image/jpeg":
|
if message.get("mime_type") == "image/jpeg":
|
||||||
encoded_image = base64.b64encode(message["data"].getvalue()).decode("utf-8")
|
encoded_image = base64.b64encode(message["data"].getvalue()).decode("utf-8")
|
||||||
text = message["content"]
|
text = message["content"]
|
||||||
message["content"] = [
|
message["content"] = [
|
||||||
{"type": "text", "text": text},
|
{"type": "text", "text": text},
|
||||||
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
|
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
|
||||||
]
|
]
|
||||||
del message["data"]
|
del message["data"]
|
||||||
del message["mime_type"]
|
del message["mime_type"]
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
# Stream chat completions using the OpenPipe client
|
# Stream chat completions using the OpenPipe client
|
||||||
chunks = (
|
chunks = (
|
||||||
await self._client.chat.completions.create(
|
await self._client.chat.completions.create(
|
||||||
model=self._model,
|
model=self._model,
|
||||||
stream=True,
|
stream=True,
|
||||||
messages=messages,
|
messages=messages,
|
||||||
openpipe={
|
openpipe={
|
||||||
"tags": {"conversation_id": self.c_id,
|
"tags": {"conversation_id": self.c_id,
|
||||||
"prompt":self.prompt},
|
"prompt": self.prompt},
|
||||||
"log_request": True
|
"log_request": True
|
||||||
}
|
}
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
logger.debug(f"OpenPipe LLM TTFB: {time.time() - start_time}")
|
logger.debug(f"OpenPipe LLM TTFB: {time.time() - start_time}")
|
||||||
|
|
||||||
|
return chunks
|
||||||
|
|
||||||
return chunks
|
|
||||||
|
|
||||||
async def _process_context(self, context):
|
async def _process_context(self, context):
|
||||||
function_name = ""
|
function_name = ""
|
||||||
arguments = ""
|
arguments = ""
|
||||||
@@ -142,7 +152,8 @@ class BaseOpenPipeLLMService(LLMService):
|
|||||||
if context:
|
if context:
|
||||||
await self._process_context(context)
|
await self._process_context(context)
|
||||||
|
|
||||||
|
|
||||||
class OpenPipeLLMService(BaseOpenPipeLLMService):
|
class OpenPipeLLMService(BaseOpenPipeLLMService):
|
||||||
|
|
||||||
def __init__(self, model="gpt-4o", cli_id=None, **kwargs):
|
def __init__(self, model="gpt-4o", cli_id=None, **kwargs):
|
||||||
super().__init__(model, cli_id, **kwargs)
|
super().__init__(model, cli_id, **kwargs)
|
||||||
|
|||||||
Reference in New Issue
Block a user