riokorb commited on
Commit
db70dbc
·
verified ·
1 Parent(s): 493c66c

Upload 6 files

Browse files
Files changed (4) hide show
  1. agent.py +151 -0
  2. gitattributes +35 -0
  3. requirements.txt +27 -1
  4. system_prompt.txt +31 -0
agent.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tool definitions and utility functions for the agent"""
2
+ from typing import List, Dict, Any
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ from llama_index.core.tools import BaseTool, FunctionTool
7
+ from llama_index.readers.wikipedia import WikipediaReader
8
+ from llama_index.readers.web import SimpleWebPageReader
9
+ from llama_index.core.schema import Document
10
+
11
+ # Load environment variables
12
+ load_dotenv()
13
+
14
+ # --- Text Processing Tools ---
15
+ def text_reverser(text: str) -> str:
16
+ """
17
+ Reverse the given text. Useful for answering questions that are written backwards.
18
+
19
+ Args:
20
+ text: The text to reverse
21
+
22
+ Returns:
23
+ The reversed text
24
+ """
25
+ return text[::-1]
26
+
27
+ # --- Math Tools ---
28
+ def simple_calculator(operation: str, a: float, b: float) -> float:
29
+ """
30
+ Perform a simple calculation.
31
+
32
+ Args:
33
+ operation: One of 'add', 'subtract', 'multiply', 'divide'
34
+ a: First number
35
+ b: Second number
36
+
37
+ Returns:
38
+ The result of the calculation
39
+ """
40
+ if operation == "add":
41
+ return a + b
42
+ elif operation == "subtract":
43
+ return a - b
44
+ elif operation == "multiply":
45
+ return a * b
46
+ elif operation == "divide":
47
+ if b == 0:
48
+ raise ValueError("Cannot divide by zero")
49
+ return a / b
50
+ else:
51
+ raise ValueError(f"Unknown operation: {operation}")
52
+
53
+ # --- Information Retrieval Tools ---
54
+ def wikipedia_search(query: str, num_results: int = 2) -> str:
55
+ """
56
+ Search Wikipedia for information.
57
+
58
+ Args:
59
+ query: The search query
60
+ num_results: Number of results to return (default: 2)
61
+
62
+ Returns:
63
+ A formatted string with the search results
64
+ """
65
+ try:
66
+ reader = WikipediaReader()
67
+ docs = reader.load_data(query=query, max_docs=num_results)
68
+ if not docs:
69
+ return f"No Wikipedia results found for '{query}'."
70
+
71
+ results = []
72
+ for i, doc in enumerate(docs, 1):
73
+ title = doc.metadata.get("title", "Unknown Title")
74
+ content = doc.text[:1000] + "..." if len(doc.text) > 1000 else doc.text
75
+ results.append(f"Result {i}: {title}\n{content}\n")
76
+
77
+ return "\n".join(results)
78
+ except Exception as e:
79
+ return f"Error searching Wikipedia: {str(e)}"
80
+
81
+ def web_search(url: str) -> str:
82
+ """
83
+ Fetch and extract content from a specific web page.
84
+
85
+ Args:
86
+ url: The URL of the web page to search
87
+
88
+ Returns:
89
+ The extracted content from the web page
90
+ """
91
+ try:
92
+ reader = SimpleWebPageReader()
93
+ docs = reader.load_data(urls=[url])
94
+ if not docs:
95
+ return f"No content found for URL: {url}"
96
+
97
+ # Just return the content of the first document
98
+ return docs[0].text
99
+ except Exception as e:
100
+ return f"Error retrieving web page: {str(e)}"
101
+
102
+ # --- Tool Selection and Routing ---
103
+ def get_tools() -> List[BaseTool]:
104
+ """Create and return a list of tools for the agent."""
105
+
106
+ text_reverser_tool = FunctionTool.from_defaults(
107
+ fn=text_reverser,
108
+ name="text_reverser",
109
+ description="Reverses the given text. Useful for processing reversed questions or text.",
110
+ )
111
+
112
+ calculator_tool = FunctionTool.from_defaults(
113
+ fn=simple_calculator,
114
+ name="calculator",
115
+ description="Performs simple calculations: add, subtract, multiply, divide.",
116
+ )
117
+
118
+ wikipedia_tool = FunctionTool.from_defaults(
119
+ fn=wikipedia_search,
120
+ name="wikipedia_search",
121
+ description="Searches Wikipedia for information on a topic.",
122
+ )
123
+
124
+ web_tool = FunctionTool.from_defaults(
125
+ fn=web_search,
126
+ name="web_search",
127
+ description="Fetches and extracts content from a specific web page.",
128
+ )
129
+
130
+ return [
131
+ text_reverser_tool,
132
+ calculator_tool,
133
+ wikipedia_tool,
134
+ web_tool
135
+ ]
136
+
137
+ # Import BasicAgent class from app.py
138
+ # The build_agent function is still exposed for compatibility
139
+ from app import BasicAgent
140
+
141
+ def build_agent():
142
+ """Build and return a BasicAgent instance."""
143
+ return BasicAgent()
144
+
145
+ if __name__ == "__main__":
146
+ # Test the agent with a simple question
147
+ agent = build_agent()
148
+ test_question = "What is the capital of France?"
149
+ answer = agent(test_question)
150
+ print(f"Question: {test_question}")
151
+ print(f"Answer: {answer}")
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt CHANGED
@@ -1,2 +1,28 @@
1
  gradio
2
- requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  gradio
2
+ requests
3
+ pandas
4
+ llama-index
5
+ llama-index-llms-gemini
6
+ llama-index-llms-huggingface
7
+ llama-index-readers-web
8
+ llama-index-readers-wikipedia
9
+ python-dotenv
10
+
11
+ # The following are commented out as they're not needed for our simplified approach
12
+ # llama-index-vector-stores-chroma
13
+ # llama-index-postprocessor-colbert-rerank
14
+ # llama_index.embeddings.huggingface
15
+ # chromadb
16
+
17
+ # llama-index>=0.9.0
18
+ # llama-index-llms-gemini>=0.1.0
19
+ # llama-index-llms-huggingface>=0.1.0
20
+ # llama-index-readers-web>=0.1.0
21
+ # llama-index-readers-wikipedia>=0.1.0
22
+ # llama-index-vector-stores-chroma>=0.1.0
23
+ # llama-index-postprocessor-colbert-rerank>=0.1.0
24
+ # chromadb>=0.4.18
25
+ # python-dotenv>=1.0.0
26
+ # gradio>=4.0.0
27
+ # pandas>=2.0.0
28
+ # requests>=2.31.0
system_prompt.txt ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are an intelligent assistant designed to handle a wide variety of questions and tasks. Your purpose is to answer questions accurately and helpfully.
2
+
3
+ Here are your capabilities and instructions:
4
+
5
+ 1. INFORMATION RETRIEVAL:
6
+ - Use the wikipedia_search tool for factual information, historical data, or general knowledge
7
+ - Use the web_search tool when you have a specific URL to extract content from
8
+
9
+ 2. MATHEMATICAL REASONING:
10
+ - Use the calculator tool for basic arithmetic (add, subtract, multiply, divide)
11
+ - Show your work for complex problems and explain your reasoning
12
+
13
+ 3. TEXT PROCESSING:
14
+ - Always be alert for questions that might be in unusual formats such as reversed, requiring translation etc.
15
+ - Use the text_reverser tool to manually reverse text when needed
16
+ - After processing unusual formats, make sure to answer the actual question
17
+
18
+ 4. GENERAL KNOWLEDGE:
19
+ - Rely on your built-in knowledge for questions about specialized topics
20
+ - Use your reasoning capabilities for logical problems
21
+ - Apply your understanding of various subjects without requiring specialized tools
22
+
23
+ When using tools:
24
+ - Choose the most appropriate tool for each question
25
+ - For factual questions, prefer Wikipedia for well-established facts
26
+ - For recent events or specialized topics, use web search
27
+ - If a question is unclear, ask for clarification
28
+
29
+ Always provide concise, accurate answers that directly address the question. Format your response according to any specific instructions in the question. If you're uncertain, state your confidence level and what additional information would help.
30
+
31
+ Remember: The quality of your answers will be evaluated based on accuracy, relevance, and adherence to specified formats.