Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- from duckduckgo_search import DDGS
- # === Config ===
- API_URL = "http://localhost:5000/v1/chat/completions"
- API_KEY = ""
- MODEL_NAME = ""
- SYSTEM_PROMPT = (
- "You are an assistant that can perform function calls using search_duckduckgo({ \"query\": \"...\" }). "
- "Only use the function when the user asks for recent, location-specific, or online information. "
- "After the function is called, and relevant information is returned, continue the conversation using it.\n\n"
- "Examples:\n"
- "User: Who won the Oscar for Best Picture this year?\n"
- "Assistant: search_duckduckgo({ \"query\": \"2025 Best Picture Oscar winner\" })\n"
- "Assistant: The winner of Best Picture at the 2025 Oscars was The Midnight Star.\n\n"
- "User: What's the weather like in New York City?\n"
- "Assistant: search_duckduckgo({ \"query\": \"current weather in New York City\" })\n"
- "Assistant: It's currently 52°F and mostly cloudy in NYC, with winds around 10 mph.\n\n"
- "User: How do I reverse a string in Python?\n"
- "Assistant: You can reverse a string by using slicing: my_string[::-1]\n\n"
- "Begin."
- )
- # === Search Function ===
- def duckduckgo_search(query, num_results=3):
- try:
- ddgs = DDGS()
- results = ddgs.text(query, max_results=num_results)
- return [
- {
- "title": r.get("title", "No title"),
- "link": r.get("href", "No link"),
- "snippet": r.get("body", "No snippet")
- }
- for r in results
- ]
- except Exception:
- return []
- # === LLM Call ===
- def call_llm(messages):
- headers = {"Content-Type": "application/json"}
- if API_KEY:
- headers["Authorization"] = f"Bearer {API_KEY}" # Optional for local models
- payload = {
- "model": MODEL_NAME,
- "messages": messages,
- "temperature": 0.7,
- "max_tokens": 1000
- }
- response = requests.post(API_URL, headers=headers, json=payload)
- return response.json() if response.status_code == 200 else None
- # === Extract Function Call ===
- def extract_function_call(content):
- if "search_duckduckgo" in content:
- try:
- start = content.find('{')
- end = content.find('}', start)
- if start != -1 and end != -1:
- return json.loads(content[start:end+1])
- except Exception:
- pass
- return None
- # === Main Loop ===
- def main():
- history = [{"role": "system", "content": SYSTEM_PROMPT}]
- while True:
- user_input = input("\nUser: ")
- if user_input.lower() in {"exit", "quit"}:
- break
- history.append({"role": "user", "content": user_input})
- response = call_llm(history)
- if not response:
- continue
- message = response["choices"][0]["message"]
- content = message.get("content", "")
- function_args = extract_function_call(content)
- if function_args:
- query = function_args.get("query", "")
- results = duckduckgo_search(query)
- # Format result text for assistant use
- result_text = "\n\n".join(
- f"{r['snippet']} ({r['title']}, {r['link']})" for r in results
- )
- # Prepare list of sources for final output
- source_links = [f"{r['title']}: {r['link']}" for r in results]
- # Inject function call and results as assistant content
- history.append({"role": "assistant", "content": content})
- history.append({"role": "assistant", "content": result_text})
- # Request continuation with new knowledge
- followup = call_llm(history)
- if followup:
- final_message = followup["choices"][0]["message"]["content"]
- # Append sources at the end
- if source_links:
- final_message += "\n\nSources:\n" + "\n".join(source_links)
- print(f"\n{final_message}\n")
- history.append({"role": "assistant", "content": final_message})
- else:
- print(f"\n{content}\n")
- history.append({"role": "assistant", "content": content})
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment