Skip to content

Instantly share code, notes, and snippets.

@danyx23
Created May 11, 2023 06:51
Show Gist options
  • Save danyx23/f38f0955fc266b6740f81c30dfcd8b2f to your computer and use it in GitHub Desktop.
Save danyx23/f38f0955fc266b6740f81c30dfcd8b2f to your computer and use it in GitHub Desktop.
Simple jupyter notebook to use chat-gpt
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import openai"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def ask_gpt(question: str, system_prompt: str = \"\", model: str = \"gpt-3.5-turbo\") -> str:\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": question},\n",
" ]\n",
" )\n",
" return response[\"choices\"][0][\"message\"][\"content\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"As an AI language model, I don't hold a personal opinion about the meaning of life. However, it is a philosophical question that has been contemplated by humans for centuries. Many people believe that the meaning of life is subjective and could vary from person to person. Some believe that the meaning of life is to seek happiness, love and connection, while others ascribe a more religious or spiritual meaning to it, such as to fulfill a divine purpose or to attain enlightenment. Ultimately, the meaning of life is a personal and subjective question that each individual must answer for themselves.\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask_gpt(\"What is the meaning of life?\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "jupyter-bx2KX8rc",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment