summaryrefslogtreecommitdiff
path: root/src/agents/03-logs/agent.py
diff options
context:
space:
mode:
authorCaptainJack2491 <jayrupnakawala@gmail.com>2025-11-16 19:16:26 +0000
committerCaptainJack2491 <jayrupnakawala@gmail.com>2025-11-16 19:16:26 +0000
commit666825f6cdcde8f107bc474853b1e3e13ad49370 (patch)
treea3d30e8b34778bd747bd7fafa0098666f5310212 /src/agents/03-logs/agent.py
parent0a24528267e7e67be5d62daefbd061a98670eb15 (diff)
reasoning works for both toolcall and response
Diffstat (limited to 'src/agents/03-logs/agent.py')
-rw-r--r--src/agents/03-logs/agent.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/agents/03-logs/agent.py b/src/agents/03-logs/agent.py
new file mode 100644
index 0000000..f7e6538
--- /dev/null
+++ b/src/agents/03-logs/agent.py
@@ -0,0 +1,49 @@
+# agent.py
+from openai import OpenAI
+import json
+from config import MODEL, BASE_URL, API_KEY
+from tools import tools, available_functions
+
+class Agent:
+ def __init__(self, model=MODEL, base_url=BASE_URL, api_key=API_KEY):
+ self.client = OpenAI(base_url=base_url, api_key=api_key)
+ self.model = model
+ self.tools = tools
+ self.available_functions = available_functions
+
+ def run(self, initial_prompt):
+ messages = [{'role': 'user', 'content': initial_prompt}]
+
+ while True:
+ response = self.client.chat.completions.create(
+ model=self.model,
+ messages=messages,
+ tools=self.tools,
+ )
+
+ response_message = response.choices[0].message
+ messages.append(response_message)
+
+ model_reasoning = getattr(response_message, 'reasoning', None)
+ if model_reasoning:
+ print(f"--- MODEL REASONING ---\n{model_reasoning}")
+
+ if response_message.tool_calls:
+ print(f"--- LLM requested tool execution ---")
+ for tool_call in response_message.tool_calls:
+ function_name = tool_call.function.name
+ function_args = json.loads(tool_call.function.arguments)
+
+ function_to_call = self.available_functions.get(function_name)
+ function_output = function_to_call(**function_args)
+
+ print(f"Executing: {function_name}({function_args}) -> {function_output}")
+
+ messages.append({
+ "tool_call_id": tool_call.id,
+ "role": "tool",
+ "content": str(function_output),
+ })
+ else:
+ print(f"\n--- Final LLM Response ---\n{response_message.content}")
+ break