mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-07-29 07:14:20 +00:00
add EventLogger for inference
This commit is contained in:
parent
7574ffb25f
commit
bbfd8a587e
2 changed files with 38 additions and 3 deletions
|
@ -13,6 +13,7 @@ from .api import (
|
|||
Inference,
|
||||
UserMessage,
|
||||
)
|
||||
from .event_logger import EventLogger
|
||||
|
||||
|
||||
class InferenceClient(Inference):
|
||||
|
@ -56,14 +57,15 @@ async def run_main(host: str, port: int):
|
|||
messages=[message],
|
||||
stream=True,
|
||||
)
|
||||
async for event in client.chat_completion(
|
||||
iterator = client.chat_completion(
|
||||
ChatCompletionRequest(
|
||||
model=InstructModel.llama3_8b_chat,
|
||||
messages=[message],
|
||||
stream=True,
|
||||
)
|
||||
):
|
||||
print(event)
|
||||
)
|
||||
async for log in EventLogger().log(iterator):
|
||||
log.print()
|
||||
|
||||
|
||||
def main(host: str, port: int):
|
||||
|
|
33
llama_toolchain/inference/event_logger.py
Normal file
33
llama_toolchain/inference/event_logger.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
|
||||
from termcolor import cprint
|
||||
from llama_toolchain.inference.api import (
|
||||
ChatCompletionResponseEventType,
|
||||
)
|
||||
|
||||
|
||||
class LogEvent:
|
||||
def __init__(
|
||||
self,
|
||||
content: str = "",
|
||||
end: str = "\n",
|
||||
color="white",
|
||||
):
|
||||
self.content = content
|
||||
self.color = color
|
||||
self.end = "\n" if end is None else end
|
||||
|
||||
def print(self, flush=True):
|
||||
cprint(f"{self.content}", color=self.color, end=self.end, flush=flush)
|
||||
|
||||
|
||||
class EventLogger:
|
||||
async def log(self, event_generator, stream=True):
|
||||
async for chunk in event_generator:
|
||||
event = chunk.event
|
||||
if event.event_type == ChatCompletionResponseEventType.start:
|
||||
yield LogEvent("Assistant> ", color="cyan", end="")
|
||||
elif event.event_type == ChatCompletionResponseEventType.progress:
|
||||
yield LogEvent(event.delta, color="yellow", end="")
|
||||
elif event.event_type == ChatCompletionResponseEventType.complete:
|
||||
yield LogEvent("")
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue