Created
May 11, 2023 06:51
-
-
Save danyx23/f38f0955fc266b6740f81c30dfcd8b2f to your computer and use it in GitHub Desktop.
Simple jupyter notebook to use chat-gpt
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
{ | |
"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