{"id":1831,"date":"2024-10-24T15:27:11","date_gmt":"2024-10-24T15:27:11","guid":{"rendered":"http:\/\/3.10.207.114\/?p=1831"},"modified":"2025-02-07T12:02:16","modified_gmt":"2025-02-07T12:02:16","slug":"openai-chat-bot-from-cli","status":"publish","type":"post","link":"https:\/\/tech-musing.com\/2024\/10\/24\/openai-chat-bot-from-cli\/","title":{"rendered":"OpenAi chat with a bot from Linux CLI"},"content":{"rendered":"\n<p>Available in Github at https:\/\/github.com\/herepete\/Fidelity\/blob\/main\/chat_with_openai.py<\/p>\n\n\n\n<p>A standalone script to allow you at the command line to chat with OpenAI<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;ec2-user@ip-172-31-33-224 Fidelity]$ cat chat_with_openai.py\n#!\/usr\/bin\/python3\nimport openai\nimport os\n\n\n# Your OpenAI API key\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\ndef chat_with_ai():\n    messages = &#91;\n        {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}\n    ]\n\n    print(\"Start chatting with the AI! Type 'exit' to end the conversation.\")\n    while True:\n        # Get user input\n        user_input = input(\"You: \")\n\n        # Exit the chat loop if the user types 'exit'\n        if user_input.lower() == 'exit':\n            print(\"Ending the conversation. Goodbye!\")\n            break\n\n        # Add the user's message to the conversation history\n        messages.append({\"role\": \"user\", \"content\": user_input})\n\n        # Get the AI's response\n        response = openai.ChatCompletion.create(\n            model=\"gpt-3.5-turbo\",\n            messages=messages,\n            max_tokens=150  # Set the maximum number of tokens for each response\n        )\n\n        # Extract and print the AI's response\n        ai_response = response&#91;'choices']&#91;0]&#91;'message']&#91;'content']\n        print(f\"AI: {ai_response}\")\n\n        # Add the AI's response to the conversation history\n        messages.append({\"role\": \"assistant\", \"content\": ai_response})\n\n\nchat_with_ai()\n<\/code><\/pre>\n\n\n\n<p>Sample Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>]# .\/chat_with_openai.py\nStart chatting with the AI! Type 'exit' to end the conversation.\nYou: Tell me  a Joke\nAI: Sure, here's a joke for you:\n\nWhy couldn't the bicycle stand up by itself?\n\nBecause it was two-tired!\nYou: nice one, tell me an intresting fact?\nAI: Here's an interesting fact for you:\n\nHoney never spoils! Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3000 years old and still perfectly edible. Honey's high sugar content and low moisture levels create an environment that prevents the growth of bacteria and yeasts, making it one of the only foods that never spoils.\nYou:<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Available in Github at https:\/\/github.com\/herepete\/Fidelity\/blob\/main\/chat_with_openai.py A standalone script to allow you at the command line to chat with OpenAI Sample Run<\/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-1831","post","type-post","status-publish","format-standard","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1831","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=1831"}],"version-history":[{"count":1,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1831\/revisions"}],"predecessor-version":[{"id":1855,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/posts\/1831\/revisions\/1855"}],"wp:attachment":[{"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/media?parent=1831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/categories?post=1831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech-musing.com\/wp-json\/wp\/v2\/tags?post=1831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}