{"id":1835,"date":"2024-10-25T12:12:58","date_gmt":"2024-10-25T12:12:58","guid":{"rendered":"http:\/\/3.10.207.114\/?p=1835"},"modified":"2025-02-07T12:02:15","modified_gmt":"2025-02-07T12:02:15","slug":"ai-agents","status":"publish","type":"post","link":"https:\/\/tech-musing.com\/2024\/10\/25\/ai-agents\/","title":{"rendered":"AI Agents"},"content":{"rendered":"\n<p>Playing around with OpenAI Swarm and ChatGBT and this was the result (although as you can see i went off on a tangent not using Swarm) but a fun excercise.<\/p>\n\n\n\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/1gx5zmdIcJwwKIvDmNRoJmqpdeLh6UnCN?usp=sharing#scrollTo=4k3_qWopAGE_\">https:\/\/colab.research.google.com\/drive\/1gx5zmdIcJwwKIvDmNRoJmqpdeLh6UnCN?usp=sharing#scrollTo=4k3_qWopAGE_<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/openai\/swarm\">https:\/\/github.com\/openai\/swarm<\/a><\/p>\n\n\n\n<p>I have uploaded to <a href=\"https:\/\/github.com\/herepete\/Ai_playing\/blob\/main\/ai_agents.py\">https:\/\/github.com\/herepete\/Ai_playing\/blob\/main\/ai_agents.py<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>See my other AI posts about putting the OpenAI key in as a variable \ud83d\ude42<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat ai_agents.py\n#!\/usr\/bin\/python3.11\n\nimport openai\nimport os\nos.system('clear')\n\nOPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n\n# Function to determine the most appropriate agent to respond\ndef select_agent(agents, user_input):\n    if \"joke\" in user_input.lower():\n        return \"Joke Creator\"\n    elif \"fact\" in user_input.lower() or \"verify\" in user_input.lower():\n        return \"Fact Checker\"\n    else:\n        return \"Creative Thinker\"\n\n# Function for the agent to respond based on instructions\ndef agent_respond(agent, context):\n    try:\n        # Make the call to the OpenAI API with clear and explicit structure\n        response = openai.ChatCompletion.create(\n            model=\"gpt-3.5-turbo\",\n            messages=&#91;\n                {\"role\": \"system\", \"content\": agent&#91;\"instructions\"]},\n                *context\n            ],\n            max_tokens=150\n        )\n        return response&#91;'choices']&#91;0]&#91;'message']&#91;'content'].strip()\n    except Exception as e:\n        print(f\"Error: {e}\")\n        return None\n\n# Function to create an agent\ndef create_agent(name, role, instructions):\n    return {\"name\": name, \"role\": role, \"instructions\": instructions}\n\n# Create agents\nagent_1 = create_agent(\"Fact Checker\", \"assistant\", \"You are a detailed fact-checker. Provide accurate and concise responses.\")\nagent_2 = create_agent(\"Creative Thinker\", \"assistant\", \"You are a creative agent that provides out-of-the-box thinking.\")\nagent_3 = create_agent(\"Joke Creator\", \"assistant\", \"You are a joke creator. Provide funny jokes when asked.\")\n\n# List of agents\nagents = &#91;agent_1, agent_2, agent_3]\n\n# Initial explanation to the user\nprint(\"Welcome! We have three agents here to assist you:\")\nprint(\"1. Fact Checker: This agent helps with verifying information, providing accurate answers, and fact-checking.\")\nprint(\"2. Creative Thinker: This agent helps with brainstorming ideas, creative problem-solving, and thinking outside the box.\")\nprint(\"3. Joke Creator: This agent helps you by creating jokes and providing humor.\")\nprint(\"Feel free to ask any questions, and our most suitable agent will assist you.\")\n\n# Run an interactive conversation loop\nwhile True:\n    # Ask user for input\n    user_input = input(\"\\nWhat do you need help with today?\\nYou: \")\n\n    # Break loop if user wants to quit\n    if user_input.lower() in &#91;\"quit\", \"exit\"]:\n        print(\"Ending the conversation.\")\n        break\n\n    # Determine the most appropriate agent based on user input\n    selected_agent_name = select_agent(agents, user_input)\n    selected_agent = next(agent for agent in agents if agent&#91;\"name\"] == selected_agent_name)\n\n    # Reset messages to contain only the most recent user input for new prompts\n    messages = &#91;{\"role\": \"user\", \"content\": user_input}]\n\n    # Run the selected agent to process the current context\n    response = agent_respond(selected_agent, messages)\n    if response:\n        messages.append({\"role\": \"assistant\", \"content\": f\"{selected_agent&#91;'name']} response: {response}\"})\n        print(f\"{selected_agent&#91;'name']} response: {response}\")\n    else:\n        print(f\"No response from {selected_agent&#91;'name']}.\")\n<\/code><\/pre>\n\n\n\n<p>result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome! We have three agents here to assist you:\n1. Fact Checker: This agent helps with verifying information, providing accurate answers, and fact-checking.\n2. Creative Thinker: This agent helps with brainstorming ideas, creative problem-solving, and thinking outside the box.\n3. Joke Creator: This agent helps you by creating jokes and providing humor.\nFeel free to ask any questions, and our most suitable agent will assist you.\n\nWhat do you need help with today?\nYou: tell me a joke\nJoke Creator response: Why did the scarecrow win an award? Because he was outstanding in his field!\n\nWhat do you need help with today?\nYou: tell me a fact\nFact Checker response: Fact: The Earth is the only planet in our solar system known to support life.\n\nWhat do you need help with today?\nYou: what is the meaning of life?\nCreative Thinker response: The meaning of life is a deeply personal and subjective question that can have many different answers depending on individual beliefs, experiences, and perspectives. Some people find meaning in pursuing their passions, building connections with others, contributing to society, seeking personal growth, exploring spirituality, or simply in enjoying the present moment. Ultimately, the meaning of life is something that each person must decide for themselves based on what brings them fulfillment and purpose.\n\nWhat do you need help with today?\nYou:\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Playing around with OpenAI Swarm and ChatGBT and this was the result (although as you can see i went off on a tangent not using Swarm) but a fun excercise. https:\/\/colab.research.google.com\/drive\/1gx5zmdIcJwwKIvDmNRoJmqpdeLh6UnCN?usp=sharing#scrollTo=4k3_qWopAGE_ https:\/\/github.com\/openai\/swarm I have uploaded to https:\/\/github.com\/herepete\/Ai_playing\/blob\/main\/ai_agents.py See my other AI posts about putting the OpenAI key in as a variable \ud83d\ude42 result<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1835","post","type-post","status-publish","format-standard","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/comments?post=1835"}],"version-history":[{"count":1,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1835\/revisions"}],"predecessor-version":[{"id":1854,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1835\/revisions\/1854"}],"wp:attachment":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/media?parent=1835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/categories?post=1835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/tags?post=1835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}