Skip to content

Commit

Permalink
Added example
Browse files Browse the repository at this point in the history
Had to manually post-launch install python_dotenv and openai and the module will work
  • Loading branch information
nitya committed Sep 26, 2023
1 parent 663df89 commit 4a9438d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/post-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# Install the OpenAI packages for Node.js
# (Python related dependencies are covered in requirements.txt)
echo "Installing OpenAI"
npm install --save openai
# echo "Installing OpenAI For Node.js"
# npm install --save openai
26 changes: 17 additions & 9 deletions 4-prompt-engineering-fundamentals/1-introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,10 @@
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'openai'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/workspaces/generative-ai-for-beginners/4-prompt-engineering-fundamentals/1-introduction.ipynb Cell 13\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell://codespaces%2Bfluffy-space-spork-4wv596rqjh7rxp/workspaces/generative-ai-for-beginners/4-prompt-engineering-fundamentals/1-introduction.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mopenai\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell://codespaces%2Bfluffy-space-spork-4wv596rqjh7rxp/workspaces/generative-ai-for-beginners/4-prompt-engineering-fundamentals/1-introduction.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mos\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell://codespaces%2Bfluffy-space-spork-4wv596rqjh7rxp/workspaces/generative-ai-for-beginners/4-prompt-engineering-fundamentals/1-introduction.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a>\u001b[0m \u001b[39m# Expects OPENAI_API_KEY in env variables \u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell://codespaces%2Bfluffy-space-spork-4wv596rqjh7rxp/workspaces/generative-ai-for-beginners/4-prompt-engineering-fundamentals/1-introduction.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39m# For GitHub Codespaces: set this as Codespaces secret => shows up as env var in OS\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell://codespaces%2Bfluffy-space-spork-4wv596rqjh7rxp/workspaces/generative-ai-for-beginners/4-prompt-engineering-fundamentals/1-introduction.ipynb#X15sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39m# For Docker Desktop: create a .env file (and .gitignore it explicitly to be safe) => shows up as env var from load_dotenv\u001b[39;00m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'openai'"
"name": "stdout",
"output_type": "stream",
"text": [
"Jupiter is a really big planet that is fifth from the Sun. It is made of gas and is the largest planet in our Solar System. It is much smaller than the Sun, but much bigger than all the other planets combined. People have known about Jupiter for a really long time because it is very bright in the night sky. It is named after a god from ancient Rome. Sometimes, Jupiter is so bright that it can make shadows on Earth. It is usually the third-brightest thing we can see at night, after the Moon and Venus.\n"
]
}
],
Expand All @@ -187,7 +183,10 @@
"# Example: have both OPENAI_API_KEY (for OpenAI) and AOAI_API_KEY (for Azure OpenAI) as options \n",
"openai.api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# Helper function to print OpenAI API response\n",
"# Print Environment Variables\n",
"#for var in os.environ:\n",
"# print(f\"{var}: {os.environ[var]}\")\n",
"\n",
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
" response = openai.ChatCompletion.create(\n",
Expand Down Expand Up @@ -225,6 +224,15 @@
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Response should have been something like:\n",
"\n",
"> Jupiter is a really big planet that is fifth from the Sun. It is made of gas and is the largest planet in our Solar System. It is much smaller than the Sun, but much bigger than all the other planets combined. People have known about Jupiter for a really long time because it is very bright in the night sky. It is named after a god from ancient Rome. Sometimes, Jupiter is so bright that it can make shadows on Earth. It is usually the third-brightest thing we can see at night, after the Moon and Venus."
]
}
],
"metadata": {
Expand Down

0 comments on commit 4a9438d

Please sign in to comment.