Skip to content

Instantly share code, notes, and snippets.

@xarical
Last active April 19, 2025 19:17
Show Gist options
  • Select an option

  • Save xarical/435e45e75711321e4e6cfa774963e0d1 to your computer and use it in GitHub Desktop.

Select an option

Save xarical/435e45e75711321e4e6cfa774963e0d1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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