Skip to content

Instantly share code, notes, and snippets.

@EgorBron
Last active October 5, 2023 15:32
Show Gist options
  • Save EgorBron/3463c4004558700c019b4c11b10f85ae to your computer and use it in GitHub Desktop.
Save EgorBron/3463c4004558700c019b4c11b10f85ae to your computer and use it in GitHub Desktop.
Usage of gpt4free in Google Colab
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOSUGPiX/7RSARLjPNmpYPo",
"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/EgorBron/3463c4004558700c019b4c11b10f85ae/g4f.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "voDGaunAqYs0"
},
"outputs": [],
"source": [
"!pip install g4f"
]
},
{
"cell_type": "code",
"source": [
"cache = []\n",
"%store cache"
],
"metadata": {
"id": "CJUGJyFeac-I"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import g4f\n",
"# @title Enter your input\n",
"text = input()\n",
"store = True # @param {type:\"boolean\"}\n",
"%store -r cache\n",
"cache.append({\"role\": \"user\", \"content\": text})\n",
"response = g4f.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=cache,\n",
")\n",
"print(response)\n",
"cache.append({\"role\": \"assistant\", \"content\": response})\n",
"if store:\n",
" %store cache"
],
"metadata": {
"id": "cYkphVRtqbW1"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment