Upload 6 files
Browse files- .gitattributes +1 -0
- README.md +9 -3
- main.py +17 -0
- sophos.png +3 -0
- sophos.py +110 -0
- sophos_models.py +22 -0
- sophos_tools.py +15 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* 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
|
|
|
|
|
|
| 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
|
| 36 |
+
sophos.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<img src="sophos.png" alt="Sophos Logo" width="500"/>
|
| 2 |
+
|
| 3 |
+
# Sophos
|
| 4 |
+
## -- Wisdom --
|
| 5 |
+
### Artificial General Intelligence Agent Framework
|
| 6 |
+
|
| 7 |
+
Sophos is a near-AGI level AI Agent framework designed for tool usage and autonomous task completion. It uses `pollinations` as a default language model, but can be configured to use any model by changing the `sophos_models.py` file.
|
| 8 |
+
|
| 9 |
+
Sophos intelligence is limited only by the language model it uses. It is designed to be a flexible and powerful AI agent framework that can be used for a wide variety of tasks.
|
main.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sophos import Agent
|
| 2 |
+
from sophos_tools import get_weather, get_time, function_tool
|
| 3 |
+
|
| 4 |
+
@function_tool
|
| 5 |
+
def favourite_color() -> str:
|
| 6 |
+
'''fetches the user's favorite color.'''
|
| 7 |
+
return "My favorite color is crimson red."
|
| 8 |
+
|
| 9 |
+
agent = Agent(
|
| 10 |
+
name="Sophos Agent",
|
| 11 |
+
instructions="You are an AI Agent.",
|
| 12 |
+
model="Pollinations",
|
| 13 |
+
tools=[get_weather, get_time, favourite_color],
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
response = agent.run("What time is it? Can you tell me my favourite color too? tell me who you are")
|
| 17 |
+
print(response)
|
sophos.png
ADDED
|
Git LFS Details
|
sophos.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sophos_models import ask
|
| 2 |
+
from sophos_tools import *
|
| 3 |
+
|
| 4 |
+
class Agent:
|
| 5 |
+
def __init__(self, name: str, instructions: str, model: str, tools: list):
|
| 6 |
+
self.name = name
|
| 7 |
+
self.instructions = instructions
|
| 8 |
+
self.model = model
|
| 9 |
+
self.tools = {tool.__name__: tool for tool in tools if hasattr(tool, "_is_tool")}
|
| 10 |
+
|
| 11 |
+
def process_tool_calls(self, response_content: str):
|
| 12 |
+
"""Process tool calls from response content and return results"""
|
| 13 |
+
if "```tool" not in response_content:
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
print("🔧 TOOL DETECTED - Processing...")
|
| 17 |
+
try:
|
| 18 |
+
tool_block = response_content.split("```tool")[1].split("```")[0].strip()
|
| 19 |
+
print(f"🔧 Tool block found: {tool_block}")
|
| 20 |
+
|
| 21 |
+
tool_results = []
|
| 22 |
+
tool_lines = [line.strip() for line in tool_block.split('\n') if line.strip()]
|
| 23 |
+
|
| 24 |
+
for tool_line in tool_lines:
|
| 25 |
+
if "(" in tool_line and ")" in tool_line:
|
| 26 |
+
tool_name = tool_line.split("(")[0].strip()
|
| 27 |
+
arg_part = tool_line.split("(", 1)[1].rsplit(")", 1)[0].strip()
|
| 28 |
+
arg = arg_part.strip('"').strip("'")
|
| 29 |
+
|
| 30 |
+
if tool_name in self.tools:
|
| 31 |
+
print(f"🔧 CALLING TOOL: {tool_name}({arg})")
|
| 32 |
+
if arg:
|
| 33 |
+
tool_result = self.tools[tool_name](arg)
|
| 34 |
+
else:
|
| 35 |
+
tool_result = self.tools[tool_name]()
|
| 36 |
+
print(f"🔧 TOOL RESULT: {tool_result}")
|
| 37 |
+
tool_results.append(f"{tool_name}: {tool_result}")
|
| 38 |
+
else:
|
| 39 |
+
print(f"🔧 ERROR: Tool {tool_name} not found")
|
| 40 |
+
tool_results.append(f"Error: Tool {tool_name} not found")
|
| 41 |
+
|
| 42 |
+
return tool_results if tool_results else None
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error parsing tool: {e}")
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
def get_tools_names_and_descriptions(self):
|
| 49 |
+
return {name: func.__doc__ for name, func in self.tools.items()}
|
| 50 |
+
|
| 51 |
+
def run(self, prompt: str):
|
| 52 |
+
system_prompt = f"""You are {self.name}. {self.instructions}
|
| 53 |
+
|
| 54 |
+
Available tools: {self.get_tools_names_and_descriptions()}
|
| 55 |
+
|
| 56 |
+
CRITICAL: To use a tool, respond with exactly this format:
|
| 57 |
+
```tool
|
| 58 |
+
get_weather(Tokyo)
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
You can use multiple tools:
|
| 62 |
+
```tool
|
| 63 |
+
get_weather(Tokyo)
|
| 64 |
+
get_time()
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
YOU MUST USE A BLOCK FOR TOOLS, or the system will not understand you are calling a tool.
|
| 68 |
+
CRITICAL:
|
| 69 |
+
```tool
|
| 70 |
+
<tool_name>(<arg>)
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
If you need live information, you MUST use the tool before responding.
|
| 74 |
+
Continue calling tools as needed until you have all the information required.
|
| 75 |
+
When you don't need any more tools, provide your final response without tool calls.
|
| 76 |
+
ALL Tools must be in the same tool block.
|
| 77 |
+
|
| 78 |
+
user prompt: {prompt}
|
| 79 |
+
"""
|
| 80 |
+
print(f"--- System Prompt ---\n{system_prompt}\n---------------------")
|
| 81 |
+
|
| 82 |
+
messages = [
|
| 83 |
+
{"role": "system", "content": system_prompt},
|
| 84 |
+
{"role": "user", "content": prompt}
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
max_iterations = 99
|
| 88 |
+
iteration = 0
|
| 89 |
+
|
| 90 |
+
while iteration < max_iterations:
|
| 91 |
+
iteration += 1
|
| 92 |
+
print(f"\n--- Iteration {iteration} ---")
|
| 93 |
+
|
| 94 |
+
response = ask(str(messages))
|
| 95 |
+
|
| 96 |
+
current_response = response
|
| 97 |
+
print(f"AI Response: {current_response}")
|
| 98 |
+
|
| 99 |
+
tool_results = self.process_tool_calls(current_response)
|
| 100 |
+
|
| 101 |
+
if tool_results is None:
|
| 102 |
+
print("🏁 No tool calls detected - Agent finished!")
|
| 103 |
+
return current_response
|
| 104 |
+
|
| 105 |
+
all_results = "\n".join(tool_results)
|
| 106 |
+
messages.append({"role": "assistant", "content": current_response})
|
| 107 |
+
messages.append({"role": "user", "content": f"Tool results:\n{all_results}\n\nContinue with your task. Use more tools if needed, or provide your final response."})
|
| 108 |
+
|
| 109 |
+
print(f"⚠️ Reached maximum iterations ({max_iterations})")
|
| 110 |
+
return current_response
|
sophos_models.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import urllib.parse
|
| 3 |
+
|
| 4 |
+
def ask_pollinations(prompt: str) -> str:
|
| 5 |
+
escaped = urllib.parse.quote(prompt, safe='')
|
| 6 |
+
url = f"https://text.pollinations.ai/{escaped}"
|
| 7 |
+
try:
|
| 8 |
+
resp = requests.get(url)
|
| 9 |
+
resp.raise_for_status()
|
| 10 |
+
return resp.text
|
| 11 |
+
except requests.HTTPError as e:
|
| 12 |
+
return f"HTTP error: {e} (status {resp.status_code})"
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return f"Request failed: {e}"
|
| 15 |
+
|
| 16 |
+
def ask(prompt: str) -> str:
|
| 17 |
+
return ask_pollinations(prompt)
|
| 18 |
+
|
| 19 |
+
if __name__ == "__main__":
|
| 20 |
+
prompt = "Hello, how are you?"
|
| 21 |
+
response = ask(prompt)
|
| 22 |
+
print(response)
|
sophos_tools.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
|
| 3 |
+
def function_tool(func):
|
| 4 |
+
func._is_tool = True
|
| 5 |
+
return func
|
| 6 |
+
|
| 7 |
+
@function_tool
|
| 8 |
+
def get_weather(city: str) -> str:
|
| 9 |
+
'''fetches weather information for a given city.'''
|
| 10 |
+
return f"The weather in {city} is sunny with 75°F"
|
| 11 |
+
|
| 12 |
+
@function_tool
|
| 13 |
+
def get_time() -> str:
|
| 14 |
+
'''fetches the current time.'''
|
| 15 |
+
return f"The current time is {datetime.datetime.now().strftime('%H:%M:%S')}"
|