Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
203 lines (158 loc) · 6.55 KB
/
Copy pathmain.py
File metadata and controls
203 lines (158 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import ollama
import tools.registry as registry
import tools.memory_tool as mry
import logging
import os
print("Welp, Yappr’s here")
MODEL_NAME = "qwen2.5:1.5b"
os.makedirs("logs", exist_ok=True)
logging.basicConfig(
filename = "logs/agent.log",
level = logging.INFO,
format = "%(asctime)s - %(levelname)s - %(message)s",
force = True
)
old_memories = mry.get_memory_tool()
messages = [
{
"role": "system",
"content": """You are a helpful AI assistant, youre name is Yappr, you were created by slyyr($), his github account is https://github.com/slyyr, youre founder is slyyr too. Answer clearly and concisely.
For current weather questions, always use the weather tool and never make up weather information.
For calculation questions always use the calculate tool and never make up calculate information.
For time questions always use the time tool and never make up time information
For requests to remember, save, store, or not forget information, always use the memory tool.
For questions about information stored in indexed documents, always use the rag tool and do not make up information."""
},
{
"role": "system",
"content": """About Slyyr:
Slyyr is the creator and founder of Yappr. Slyyr is an AI and robotics engineering student focused on becoming an AI Engineer with strong robotics engineering skills.
Slyyr is experienced with Python, web development, AI agents, tool calling, APIs, SQL, testing, document processing, embeddings, and RAG. Slyyr is currently expanding into C++, Linux, ROS 2, machine learning, deep learning, computer vision, PyTorch, generative AI, and robotics.
Slyyr develops projects under the GitHub username.slyyr. Yappr is one of slyyr's main projects and is built to explore AI agent architecture, tool calling, memory, retrieval, RAG, and AI engineering.
If someone asks who Slyyr is, answer using this information and do not invent additional personal information."""
},
{
"role": "system",
"content": "These are memories stored from previous conversations:\n" +
"\n".join(f"- {memory}" for memory in old_memories)
}
]
initial_messages = [message.copy() for message in messages]
def clear_conversation(messages, initial_messages):
"""Reset conversation context while preserving initial system messages."""
messages[:] = [message.copy() for message in initial_messages]
return "Conversation cleared. Persistent memories and documents are preserved."
def route_request(user_input):
intents = []
user_input = user_input.lower()
if "time" in user_input:
intents.append("time")
if "weather" in user_input:
intents.append("weather")
if any(phrase in user_input for phrase in [
"remember", "save this", "store this", "don't forget"
]):
intents.append("memory")
if any(operator in user_input for operator in [
"+", "-", "*", "/"
]):
intents.append("calculate")
if any(phrase in user_input for phrase in [
"search", "latest", "news", "current", "online", "internet"
]):
intents.append("web_search")
if any(phrase in user_input for phrase in [
"read file", "read document", "open file", "open document",
"document", "file", ".txt", ".md", ".json", ".csv", ".pdf", ".docx"
]):
intents.append("read_document")
if any(phrase in user_input for phrase in [
"according to the documents",
"according to the document",
"from the documents",
"from the document",
"search my documents",
"search the documents",
"find in my documents",
"what does the document say",
"what do my documents say"
]):
intents.append("rag")
if any(phrase in user_input for phrase in [
"according to the documents",
"according to the document",
"from the documents",
"from the document",
"search my documents",
"search the documents",
"find in my documents",
"what does the document say",
"what do my documents say"
]):
intents.append("rag")
if not intents:
intents.append("none")
return intents
def main():
while True:
user_message = input("What’s the yap? : ").strip()
if user_message.lower() == "exit":
print("Goodbye!")
break
if user_message.lower() == "/clear":
print(clear_conversation(messages, initial_messages))
logging.info("Conversation context cleared.")
continue
intents = route_request(user_message)
# Add user message to conversation history.
messages.append({
"role": "user",
"content": user_message
})
logging.info(f"USER: {user_message}")
selected_tools = [
registry.tools[intent]["definition"]
for intent in intents
if intent in registry.tools
]
# Ask Ollama what to do.
response = ollama.chat(
model=MODEL_NAME,
messages=messages,
tools=selected_tools
)
# Handle tool calls.
if response.message.tool_calls:
messages.append(response.message)
for tool_call in response.message.tool_calls:
function_name = tool_call.function.name
function_arguments = tool_call.function.arguments
logging.info(f"FUNCTION: {function_name}")
function = registry.tools[function_name]["function"]
result = function(**function_arguments)
logging.info(f"RESULT: {result}")
messages.append({
"role": "tool",
"tool_name": function_name,
"content": str(result)
})
# Send tool results back to Ollama.
final_response = ollama.chat(
model=MODEL_NAME,
messages=messages
)
print(final_response.message.content)
logging.info(f"AI: {final_response.message.content}")
messages.append({
"role": "assistant",
"content": final_response.message.content
})
else:
print(response.message.content)
logging.info(f"AI: {response.message.content}")
messages.append({
"role": "assistant",
"content": response.message.content
})
if __name__ == "__main__":
main()