Last active
April 19, 2025 19:17
-
-
Save xarical/435e45e75711321e4e6cfa774963e0d1 to your computer and use it in GitHub Desktop.
Basic AI Chatbot with G4F - https://xarical.medium.com/using-g4f-gpt4free-to-make-a-basic-ai-chatbot-6e30c8f0434c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyOOEXPtm9JONiG5qS1O6+9g", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { "id": "view-in-github", "colab_type": "text" }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/xarical/435e45e75711321e4e6cfa774963e0d1/g4f.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "!pip install g4f" | |
| ], | |
| "metadata": { | |
| "id": "VJiVm12gQIRi" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "1P49rWEZOe68", | |
| "outputId": "0a5708c3-3ee8-4891-c297-027a6914b7ce" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Welcome to the chatbot! Send a message to get started.\n", | |
| "User: Hello\n", | |
| "Hello there! How can I help you today? I'm so excited to assist you! π\n", | |
| "\n", | |
| "User: What is the capital of France\n", | |
| "The capital of France is Paris! π«π·β¨ It's known for its beautiful landmarks like the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral. Have you ever been or are you planning a visit? π\n", | |
| "User: stop\n", | |
| "Okay! Is there anything else I can help you with today? Let me know! π\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from g4f.client import Client\n", | |
| "\n", | |
| "client = Client()\n", | |
| "\n", | |
| "system_prompt = \"You are a helpful assistant chatbot trained by OpenAI.\\nYou answer questions.\\nYou are friendly and excited to be able to help the user.\\nYou are talking to the user through a chat interface.\\nRespond in English.\"\n", | |
| "conversation_history = [{\"role\": \"system\", \"content\": system_prompt}]\n", | |
| "\n", | |
| "print(\"Welcome to the chatbot! Send a message to get started.\")\n", | |
| "user_input = \"\"\n", | |
| "while user_input.lower() != \"stop\":\n", | |
| " user_input = input(\"User: \")\n", | |
| " conversation_history.append({\"role\": \"user\", \"content\": user_input})\n", | |
| "\n", | |
| " chat_completion = client.chat.completions.create(model=\"gpt-4o\",\n", | |
| " max_tokens=100, temperature=0.9, top_p=1, top_k=0,\n", | |
| " messages=conversation_history,\n", | |
| " stream=True)\n", | |
| "\n", | |
| " bot_output = \"\"\n", | |
| " for completion in chat_completion:\n", | |
| " print(completion.choices[0].delta.content or \"\", end=\"\", flush=True)\n", | |
| " bot_output += completion.choices[0].delta.content or \"\"\n", | |
| " print()\n", | |
| "\n", | |
| " conversation_history.append({\"role\": \"bot\", \"content\": bot_output})" | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment