Skip to content

Commit

Permalink
Add logging for unified debug (#521)
Browse files Browse the repository at this point in the history
Signed-off-by: Xinyao Wang <xinyao.wang@intel.com>
  • Loading branch information
XinyaoWa committed Aug 20, 2024
1 parent 0584b45 commit fab1fbd
Show file tree
Hide file tree
Showing 46 changed files with 891 additions and 270 deletions.
3 changes: 3 additions & 0 deletions comps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@

# Statistics
from comps.cores.mega.base_statistics import statistics_dict, register_statistics

# Logger
from comps.cores.mega.logger import CustomLogger
13 changes: 10 additions & 3 deletions comps/agent/langchain/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
comps_path = os.path.join(cur_path, "../../../")
sys.path.append(comps_path)

from comps import GeneratedDoc, LLMParamsDoc, ServiceType, opea_microservices, register_microservice
from comps import CustomLogger, GeneratedDoc, LLMParamsDoc, ServiceType, opea_microservices, register_microservice
from comps.agent.langchain.src.agent import instantiate_agent
from comps.agent.langchain.src.utils import get_args

logger = CustomLogger("comps-react-agent")
logflag = os.getenv("LOGFLAG", False)

args, _ = get_args()


Expand All @@ -28,12 +31,16 @@
input_datatype=LLMParamsDoc,
)
async def llm_generate(input: LLMParamsDoc):
if logflag:
logger.info(input)
# 1. initialize the agent
print("args: ", args)
if logflag:
logger.info("args: ", args)
input.streaming = args.streaming
config = {"recursion_limit": args.recursion_limit}
agent_inst = instantiate_agent(args, args.strategy)
print(type(agent_inst))
if logflag:
logger.info(type(agent_inst))

# 2. prepare the input for the agent
if input.streaming:
Expand Down
12 changes: 10 additions & 2 deletions comps/asr/asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import numpy as np
import requests

from comps import CustomLogger

logger = CustomLogger("asr")
logflag = os.getenv("LOGFLAG", False)

from comps import (
Base64ByteStrDoc,
LLMParamsDoc,
Expand All @@ -33,14 +38,17 @@ async def audio_to_text(audio: Base64ByteStrDoc):
start = time.time()
byte_str = audio.byte_str
inputs = {"audio": byte_str}
if logflag:
logger.info(inputs)

response = requests.post(url=f"{asr_endpoint}/v1/asr", data=json.dumps(inputs), proxies={"http": None})

if logflag:
logger.info(response)
statistics_dict["opea_service@asr"].append_latency(time.time() - start, None)
return LLMParamsDoc(query=response.json()["asr_result"])


if __name__ == "__main__":
asr_endpoint = os.getenv("ASR_ENDPOINT", "http://localhost:7066")
print("[asr - router] ASR initialized.")
logger.info("[asr - router] ASR initialized.")
opea_microservices["opea_service@asr"].start()
24 changes: 20 additions & 4 deletions comps/chathistory/mongo/chathistory_mongo.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import os
from typing import Optional

from fastapi import HTTPException
from mongo_store import DocumentStore
from pydantic import BaseModel

from comps import CustomLogger
from comps.cores.mega.micro_service import opea_microservices, register_microservice
from comps.cores.proto.api_protocol import ChatCompletionRequest

logger = CustomLogger("chathistory_mongo")
logflag = os.getenv("LOGFLAG", False)


class ChatMessage(BaseModel):
data: ChatCompletionRequest
Expand Down Expand Up @@ -50,7 +55,8 @@ async def create_documents(document: ChatMessage):
Returns:
The result of the operation if successful, None otherwise.
"""

if logflag:
logger.info(document)
try:
if document.data.user is None:
raise HTTPException(status_code=500, detail="Please provide the user information")
Expand All @@ -62,10 +68,12 @@ async def create_documents(document: ChatMessage):
res = await store.update_document(document.id, document.data, document.first_query)
else:
res = await store.save_document(document)
if logflag:
logger.info(res)
return res
except Exception as e:
# Handle the exception here
print(f"An error occurred: {str(e)}")
logger.info(f"An error occurred: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))


Expand All @@ -85,17 +93,21 @@ async def get_documents(document: ChatId):
Returns:
The retrieved documents if successful, None otherwise.
"""
if logflag:
logger.info(document)
try:
store = DocumentStore(document.user)
store.initialize_storage()
if document.id is None:
res = await store.get_all_documents_of_user()
else:
res = await store.get_user_documents_by_id(document.id)
if logflag:
logger.info(res)
return res
except Exception as e:
# Handle the exception here
print(f"An error occurred: {str(e)}")
logger.info(f"An error occurred: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))


Expand All @@ -115,17 +127,21 @@ async def delete_documents(document: ChatId):
Returns:
The result of the deletion if successful, None otherwise.
"""
if logflag:
logger.info(document)
try:
store = DocumentStore(document.user)
store.initialize_storage()
if document.id is None:
raise Exception("Document id is required.")
else:
res = await store.delete_document(document.id)
if logflag:
logger.info(res)
return res
except Exception as e:
# Handle the exception here
print(f"An error occurred: {str(e)}")
logger.info(f"An error occurred: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))


Expand Down
2 changes: 1 addition & 1 deletion comps/cores/mega/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, name: str = None):
self.__dict__[key.lower()] = functools.partial(self.log_message, level)

# Set up log format and handler
self.format = logging.Formatter(fmt="[%(asctime)-15s] [%(levelname)8s] - %(message)s")
self.format = logging.Formatter(fmt="[%(asctime)-15s] [%(levelname)8s] - %(name)s - %(message)s")
self.handler = logging.StreamHandler()
self.handler.setFormatter(self.format)

Expand Down
Loading

0 comments on commit fab1fbd

Please sign in to comment.