Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Using ChatGPT API Key.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"source": [
"my_list = [1,2,3]\n",
"\n",
"for num in my_list\n",
"for num in my_list :\n",
" num ** 2"
]
},
Expand Down
83 changes: 83 additions & 0 deletions example_for_apisetup.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"import os\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv())\n",
"\n",
"openai.api_key = os.getenv('OPENAI_API_KEY')\n",
"# set the envinonment variable and create and set the api key.#\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=0, \n",
" # this is the degree of randomness of the model's output\n",
" )\n",
" return response.choices[0].message[\"content\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"text = f\"\"\"\n",
"You should express what you want a model to do by \\ \n",
"providing instructions that are as clear and \\ \n",
"specific as you can possibly make them. \\ \n",
"This will guide the model towards the desired output, \\ \n",
"and reduce the chances of receiving irrelevant \\ \n",
"or incorrect responses. Don't confuse writing a \\ \n",
"clear prompt with writing a short prompt. \\ \n",
"In many cases, longer prompts provide more clarity \\ \n",
"and context for the model, which can lead to \\ \n",
"more detailed and relevant outputs.\n",
"\"\"\"\n",
"prompt = f\"\"\"\n",
"Summarize the text delimited by triple backticks \\ \n",
"into a single sentence.\n",
"```{text}```\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)\n",
"# this is the prompt that needed to get the responses #"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"prompt = f\"\"\"\n",
"Generate a list of three made-up book titles along \\ \n",
"with their authors and genres. \n",
"Provide them in JSON format with the following keys: \n",
"book_id, title, author, genre.\n",
"\"\"\"\n",
"response = get_completion(prompt)\n",
"print(response)\n",
"#asking for a structured output#"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}