at least this kind of works
This commit is contained in:
63
archive/claude_tool_test.py
Normal file
63
archive/claude_tool_test.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# Made with Claude
|
||||
|
||||
import ollama
|
||||
|
||||
# Simulated database
|
||||
database = {
|
||||
"users": ["Alice", "Bob", "Charlie"],
|
||||
"products": ["Chair", "Table", "Lamp"],
|
||||
"orders": ["Order#1: Alice bought Chair", "Order#2: Bob bought Lamp"],
|
||||
}
|
||||
|
||||
def query_database(keyword):
|
||||
keyword = keyword.strip().lower()
|
||||
for key, values in database.items():
|
||||
if keyword == key.lower():
|
||||
return f"Database result for '{keyword}': {values}"
|
||||
return f"No results found for '{keyword}'"
|
||||
|
||||
system_prompt = """You are a helpful assistant with access to a database.
|
||||
If you need to look something up in the database, output EXACTLY:
|
||||
$DATABASE$
|
||||
<keyword>
|
||||
|
||||
Where <keyword> is what you want to search for (e.g. 'users', 'products', 'orders').
|
||||
Do not add anything else on those lines. Wait for the result before responding."""
|
||||
|
||||
def chat(user_input):
|
||||
messages = [
|
||||
{'role': 'system', 'content': system_prompt},
|
||||
{'role': 'user', 'content': user_input},
|
||||
]
|
||||
|
||||
while True:
|
||||
# Get response (non-streaming for easier parsing)
|
||||
response = ollama.chat(model='llama3.2', messages=messages)
|
||||
reply = response['message']['content']
|
||||
|
||||
print(f"[Raw model output]: {reply}\n")
|
||||
|
||||
# Check if the model wants to query the database
|
||||
if '$DATABASE$' in reply:
|
||||
lines = reply.strip().split('\n')
|
||||
db_index = next(i for i, l in enumerate(lines) if '$DATABASE$' in l)
|
||||
|
||||
# Keyword is on the next line
|
||||
if db_index + 1 < len(lines):
|
||||
keyword = lines[db_index + 1].strip()
|
||||
db_result = query_database(keyword)
|
||||
print(f"[Database queried: '{keyword}' → {db_result}]\n")
|
||||
|
||||
# Feed the result back into the conversation
|
||||
messages.append({'role': 'assistant', 'content': reply})
|
||||
messages.append({'role': 'user', 'content': f"Database result: {db_result}\nNow respond to the user."})
|
||||
# Loop again so the model can respond with the result
|
||||
else:
|
||||
print("Model requested DB but didn't provide a keyword.")
|
||||
break
|
||||
else:
|
||||
# No database call, just print the final response
|
||||
print(f"Assistant: {reply}")
|
||||
break
|
||||
|
||||
chat("Can you show me the list of users?")
|
||||
36
archive/tool_test.py
Normal file
36
archive/tool_test.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# TOOL USE TEST
|
||||
|
||||
import ollama
|
||||
|
||||
system_prompt = """You have access to a list. If you want to retrieve the content in the list, output EXACTLY:
|
||||
$DATABASE$
|
||||
|
||||
Do not output anything else. The result will be returned in a string starting with [DATABASE].
|
||||
"""
|
||||
context_prompt = None # if context exists
|
||||
user_input = "Check the list and tell me what is in there"
|
||||
|
||||
combined_system = f"{system_prompt}\n\nContext: {context_prompt}"
|
||||
|
||||
stream = ollama.chat(
|
||||
model='llama3.2',
|
||||
messages=[
|
||||
{'role': 'system', 'content': combined_system},
|
||||
{'role': 'user', 'content': user_input},
|
||||
],
|
||||
stream=True,
|
||||
)
|
||||
|
||||
# Accumulate chunks into a variable
|
||||
full_response = ""
|
||||
for chunk in stream:
|
||||
content = chunk['message']['content']
|
||||
print(content, end='', flush=True)
|
||||
full_response += content
|
||||
|
||||
if "$DATABASE$" in full_response:
|
||||
# DATABASE LOGIC TODO
|
||||
pass
|
||||
|
||||
print() # newline after streaming
|
||||
print(full_response) # your complete output
|
||||
Reference in New Issue
Block a user