# 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$ Where 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?")