Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework logging #3358

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions agent/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
# limitations under the License.
#
import json
import traceback
from abc import ABC
from copy import deepcopy
from functools import partial
from agent.component import component_class
from agent.component.base import ComponentBase
from agent.settings import flow_logger, DEBUG

from api.utils.log_utils import logger

class Canvas(ABC):
"""
Expand Down Expand Up @@ -189,7 +187,7 @@ def prepare2run(cpns):
if cpn.component_name == "Answer":
self.answer.append(c)
else:
if DEBUG: print("RUN: ", c)
logger.debug(f"Canvas.prepare2run: {c}")
cpids = cpn.get_dependent_components()
if any([c not in self.path[-1] for c in cpids]):
continue
Expand All @@ -199,7 +197,7 @@ def prepare2run(cpns):

prepare2run(self.components[self.path[-2][-1]]["downstream"])
while 0 <= ran < len(self.path[-1]):
if DEBUG: print(ran, self.path)
logger.debug(f"Canvas.run: {ran} {self.path}")
cpn_id = self.path[-1][ran]
cpn = self.get_component(cpn_id)
if not cpn["downstream"]: break
Expand All @@ -219,7 +217,7 @@ def prepare2run(cpns):
self.get_component(p)["obj"].set_exception(e)
prepare2run([p])
break
traceback.print_exc()
logger.exception("Canvas.run got exception")
break
continue

Expand All @@ -231,7 +229,7 @@ def prepare2run(cpns):
self.get_component(p)["obj"].set_exception(e)
prepare2run([p])
break
traceback.print_exc()
logger.exception("Canvas.run got exception")
break

if self.answer:
Expand Down
5 changes: 2 additions & 3 deletions agent/component/arxiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
from abc import ABC
import arxiv
import pandas as pd
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase

from api.utils.log_utils import logger

class ArXivParam(ComponentParamBase):
"""
Expand Down Expand Up @@ -65,5 +64,5 @@ def _run(self, history, **kwargs):
return ArXiv.be_output("")

df = pd.DataFrame(arxiv_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {str(df)}")
return df
6 changes: 2 additions & 4 deletions agent/component/baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
from abc import ABC
from functools import partial
import pandas as pd
import requests
import re
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase
from api.utils.log_utils import logger


class BaiduParam(ComponentParamBase):
Expand Down Expand Up @@ -64,6 +62,6 @@ def _run(self, history, **kwargs):
return Baidu.be_output("")

df = pd.DataFrame(baidu_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {str(df)}")
return df

14 changes: 7 additions & 7 deletions agent/component/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import builtins
import json
import os
from copy import deepcopy
from functools import partial
from typing import List, Dict, Tuple, Union
from typing import Tuple, Union

import pandas as pd

from agent import settings
from agent.settings import flow_logger, DEBUG
from api.utils.log_utils import logger


_FEEDED_DEPRECATED_PARAMS = "_feeded_deprecated_params"
_DEPRECATED_PARAMS = "_deprecated_params"
Expand Down Expand Up @@ -361,13 +361,13 @@ def _not_in(value, wrong_value_list):

def _warn_deprecated_param(self, param_name, descr):
if self._deprecated_params_set.get(param_name):
flow_logger.warning(
logger.warning(
f"{descr} {param_name} is deprecated and ignored in this version."
)

def _warn_to_deprecate_param(self, param_name, descr, new_param):
if self._deprecated_params_set.get(param_name):
flow_logger.warning(
logger.warning(
f"{descr} {param_name} will be deprecated in future release; "
f"please use {new_param} instead."
)
Expand Down Expand Up @@ -403,7 +403,7 @@ def get_dependent_components(self):
return cpnts

def run(self, history, **kwargs):
flow_logger.info("{}, history: {}, kwargs: {}".format(self, json.dumps(history, ensure_ascii=False),
logger.info("{}, history: {}, kwargs: {}".format(self, json.dumps(history, ensure_ascii=False),
json.dumps(kwargs, ensure_ascii=False)))
try:
res = self._run(history, **kwargs)
Expand Down Expand Up @@ -463,7 +463,7 @@ def get_input(self):
reversed_cpnts.extend(self._canvas.path[-2])
reversed_cpnts.extend(self._canvas.path[-1])

if DEBUG: print(self.component_name, reversed_cpnts[::-1])
logger.debug(f"{self.component_name} {reversed_cpnts[::-1]}")
for u in reversed_cpnts[::-1]:
if self.get_component_name(u) in ["switch", "concentrator"]: continue
if self.component_name.lower() == "generate" and self.get_component_name(u) == "retrieval":
Expand Down
5 changes: 2 additions & 3 deletions agent/component/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
from abc import ABC
import requests
import pandas as pd
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase

from api.utils.log_utils import logger

class BingParam(ComponentParamBase):
"""
Expand Down Expand Up @@ -81,5 +80,5 @@ def _run(self, history, **kwargs):
return Bing.be_output("")

df = pd.DataFrame(bing_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {str(df)}")
return df
6 changes: 3 additions & 3 deletions agent/component/categorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from api.db import LLMType
from api.db.services.llm_service import LLMBundle
from agent.component import GenerateParam, Generate
from agent.settings import DEBUG
from api.utils.log_utils import logger


class CategorizeParam(GenerateParam):
Expand All @@ -34,7 +34,7 @@ def check(self):
super().check()
self.check_empty(self.category_description, "[Categorize] Category examples")
for k, v in self.category_description.items():
if not k: raise ValueError(f"[Categorize] Category name can not be empty!")
if not k: raise ValueError("[Categorize] Category name can not be empty!")
if not v.get("to"): raise ValueError(f"[Categorize] 'To' of category {k} can not be empty!")

def get_prompt(self):
Expand Down Expand Up @@ -77,7 +77,7 @@ def _run(self, history, **kwargs):
chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
ans = chat_mdl.chat(self._param.get_prompt(), [{"role": "user", "content": input}],
self._param.gen_conf())
if DEBUG: print(ans, ":::::::::::::::::::::::::::::::::", input)
logger.debug(f"input: {input}, answer: {str(ans)}")
for c in self._param.category_description.keys():
if ans.lower().find(c.lower()) >= 0:
return Categorize.be_output(self._param.category_description[c]["to"])
Expand Down
4 changes: 2 additions & 2 deletions agent/component/duckduckgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from abc import ABC
from duckduckgo_search import DDGS
import pandas as pd
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase
from api.utils.log_utils import logger


class DuckDuckGoParam(ComponentParamBase):
Expand Down Expand Up @@ -62,5 +62,5 @@ def _run(self, history, **kwargs):
return DuckDuckGo.be_output("")

df = pd.DataFrame(duck_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug("df: {df}")
return df
4 changes: 2 additions & 2 deletions agent/component/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from abc import ABC
import pandas as pd
import requests
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase
from api.utils.log_utils import logger


class GitHubParam(ComponentParamBase):
Expand Down Expand Up @@ -57,5 +57,5 @@ def _run(self, history, **kwargs):
return GitHub.be_output("")

df = pd.DataFrame(github_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {df}")
return df
6 changes: 3 additions & 3 deletions agent/component/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from abc import ABC
from serpapi import GoogleSearch
import pandas as pd
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase
from api.utils.log_utils import logger


class GoogleParam(ComponentParamBase):
Expand Down Expand Up @@ -85,12 +85,12 @@ def _run(self, history, **kwargs):
"hl": self._param.language, "num": self._param.top_n})
google_res = [{"content": '<a href="' + i["link"] + '">' + i["title"] + '</a> ' + i["snippet"]} for i in
client.get_dict()["organic_results"]]
except Exception as e:
except Exception:
return Google.be_output("**ERROR**: Existing Unavailable Parameters!")

if not google_res:
return Google.be_output("")

df = pd.DataFrame(google_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {df}")
return df
8 changes: 4 additions & 4 deletions agent/component/googlescholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#
from abc import ABC
import pandas as pd
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase
from scholarly import scholarly
from api.utils.log_utils import logger


class GoogleScholarParam(ComponentParamBase):
Expand Down Expand Up @@ -58,13 +58,13 @@ def _run(self, history, **kwargs):
'pub_url'] + '"></a> ' + "\n author: " + ",".join(pub['bib']['author']) + '\n Abstract: ' + pub[
'bib'].get('abstract', 'no abstract')})

except StopIteration or Exception as e:
print("**ERROR** " + str(e))
except StopIteration or Exception:
logger.exception("GoogleScholar")
break

if not scholar_res:
return GoogleScholar.be_output("")

df = pd.DataFrame(scholar_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {df}")
return df
4 changes: 2 additions & 2 deletions agent/component/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from api.db import LLMType
from api.db.services.llm_service import LLMBundle
from agent.component import GenerateParam, Generate
from agent.settings import DEBUG
from api.utils.log_utils import logger


class KeywordExtractParam(GenerateParam):
Expand Down Expand Up @@ -58,5 +58,5 @@ def _run(self, history, **kwargs):
self._param.gen_conf())

ans = re.sub(r".*keyword:", "", ans).strip()
if DEBUG: print(ans, ":::::::::::::::::::::::::::::::::")
logger.info(f"ans: {ans}")
return KeywordExtract.be_output(ans)
4 changes: 2 additions & 2 deletions agent/component/pubmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import re
import pandas as pd
import xml.etree.ElementTree as ET
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase
from api.utils.log_utils import logger


class PubMedParam(ComponentParamBase):
Expand Down Expand Up @@ -65,5 +65,5 @@ def _run(self, history, **kwargs):
return PubMed.be_output("")

df = pd.DataFrame(pubmed_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {df}")
return df
3 changes: 2 additions & 1 deletion agent/component/relevant.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from api.db.services.llm_service import LLMBundle
from agent.component import GenerateParam, Generate
from rag.utils import num_tokens_from_string, encoder
from api.utils.log_utils import logger


class RelevantParam(GenerateParam):
Expand Down Expand Up @@ -70,7 +71,7 @@ def _run(self, history, **kwargs):
ans = chat_mdl.chat(self._param.get_prompt(), [{"role": "user", "content": ans}],
self._param.gen_conf())

print(ans, ":::::::::::::::::::::::::::::::::")
logger.info(ans)
if ans.lower().find("yes") >= 0:
return Relevant.be_output(self._param.yes)
if ans.lower().find("no") >= 0:
Expand Down
3 changes: 2 additions & 1 deletion agent/component/retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from api.db.services.llm_service import LLMBundle
from api.settings import retrievaler
from agent.component.base import ComponentBase, ComponentParamBase
from api.utils.log_utils import logger


class RetrievalParam(ComponentParamBase):
Expand Down Expand Up @@ -80,7 +81,7 @@ def _run(self, history, **kwargs):
df = pd.DataFrame(kbinfos["chunks"])
df["content"] = df["content_with_weight"]
del df["content_with_weight"]
print(">>>>>>>>>>>>>>>>>>>>>>>>>>\n", query, df)
logger.debug("{} {}".format(query, df))
return df


3 changes: 2 additions & 1 deletion agent/component/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from api.db import LLMType
from api.db.services.llm_service import LLMBundle
from agent.component import GenerateParam, Generate
from api.utils.log_utils import logger


class RewriteQuestionParam(GenerateParam):
Expand Down Expand Up @@ -104,7 +105,7 @@ def _run(self, history, **kwargs):
self._canvas.history.pop()
self._canvas.history.append(("user", ans))

print(ans, ":::::::::::::::::::::::::::::::::")
logger.info(ans)
return RewriteQuestion.be_output(ans)


6 changes: 2 additions & 4 deletions agent/component/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
from abc import ABC
from functools import partial
import wikipedia
import pandas as pd
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase
from api.utils.log_utils import logger


class WikipediaParam(ComponentParamBase):
Expand Down Expand Up @@ -65,5 +63,5 @@ def _run(self, history, **kwargs):
return Wikipedia.be_output("")

df = pd.DataFrame(wiki_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
logger.debug(f"df: {df}")
return df
Loading