Martin Bär commited on
Commit
09a402e
·
1 Parent(s): ba66f78

Only activate langfuse conditionally

Browse files
Files changed (2) hide show
  1. .gitignore +1 -1
  2. basic_agent.py +18 -8
.gitignore CHANGED
@@ -1,4 +1,4 @@
1
  __pycache__
2
  .venv
3
- tool_tests.ipynb
4
  .env
 
1
  __pycache__
2
  .venv
3
+ *.ipynb
4
  .env
basic_agent.py CHANGED
@@ -8,14 +8,20 @@ from llama_index.core.tools.tool_spec.load_and_search import LoadAndSearchToolSp
8
  from llama_index.readers.web import SimpleWebPageReader
9
  from llama_index.core.tools.ondemand_loader_tool import OnDemandLoaderTool
10
  from langfuse.llama_index import LlamaIndexInstrumentor
11
-
12
- # Langfuse
13
- instrumentor = LlamaIndexInstrumentor()
14
- instrumentor.start()
15
 
16
  class BasicAgent:
17
- def __init__(self):
18
- llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
 
 
 
 
 
 
 
 
 
19
 
20
  # Initialize tools
21
  tool_spec = DuckDuckGoSearchToolSpec()
@@ -31,6 +37,8 @@ class BasicAgent:
31
 
32
  # Convert into a LoadAndSearchToolSpec because the wikipedia search tool returns
33
  # entire Wikipedia pages and this can pollute the context window of the LLM
 
 
34
  wiki_search_tool_las = LoadAndSearchToolSpec.from_defaults(wiki_search_tool).to_tool_list()
35
 
36
  webpage_tool = OnDemandLoaderTool.from_defaults(
@@ -41,7 +49,7 @@ class BasicAgent:
41
 
42
  # Create Alfred with all the tools
43
  self.agent = AgentWorkflow.from_tools_or_functions(
44
- wiki_search_tool_las + [search_tool], # webpage_tool does not work properly
45
  llm=llm,
46
  verbose=True,
47
  system_prompt=("You are a helpful agent that can search Wikipedia and the Internet for answers. "
@@ -52,5 +60,7 @@ class BasicAgent:
52
 
53
  async def __call__(self, question: str) -> str:
54
  response = await self.agent.run(user_msg=question) # ctx=self.ctx)
55
- instrumentor.flush()
 
 
56
  return response.response.content
 
8
  from llama_index.readers.web import SimpleWebPageReader
9
  from llama_index.core.tools.ondemand_loader_tool import OnDemandLoaderTool
10
  from langfuse.llama_index import LlamaIndexInstrumentor
11
+ from llama_index.llms.ollama import Ollama
 
 
 
12
 
13
  class BasicAgent:
14
+ def __init__(self, ollama=False, langfuse=True):
15
+ if not ollama:
16
+ llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
17
+ else:
18
+ llm = Ollama(model="mistral:latest", request_timeout=120.0)
19
+
20
+ # Langfuse
21
+ self.langfuse = langfuse
22
+ if self.langfuse:
23
+ self.instrumentor = LlamaIndexInstrumentor()
24
+ self.instrumentor.start()
25
 
26
  # Initialize tools
27
  tool_spec = DuckDuckGoSearchToolSpec()
 
37
 
38
  # Convert into a LoadAndSearchToolSpec because the wikipedia search tool returns
39
  # entire Wikipedia pages and this can pollute the context window of the LLM
40
+
41
+ # TODO this does not work so well. We need to make the retriever return the top 5 chunks or sth.
42
  wiki_search_tool_las = LoadAndSearchToolSpec.from_defaults(wiki_search_tool).to_tool_list()
43
 
44
  webpage_tool = OnDemandLoaderTool.from_defaults(
 
49
 
50
  # Create Alfred with all the tools
51
  self.agent = AgentWorkflow.from_tools_or_functions(
52
+ wiki_search_tool_las + [search_tool], # webpage_tool does not work properly - cookies etc
53
  llm=llm,
54
  verbose=True,
55
  system_prompt=("You are a helpful agent that can search Wikipedia and the Internet for answers. "
 
60
 
61
  async def __call__(self, question: str) -> str:
62
  response = await self.agent.run(user_msg=question) # ctx=self.ctx)
63
+
64
+ if self.langfuse:
65
+ self.instrumentor.flush()
66
  return response.response.content