Skip to content

Commit

Permalink
Merge pull request #1 from DanielMeixner/main
Browse files Browse the repository at this point in the history
Add notebook and requirements file for Phase 1
  • Loading branch information
denniszielke authored May 22, 2024
2 parents 7bb8094 + 9e1fa01 commit 09f22ee
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
8 changes: 8 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
langchain==0.2.0
langchain-community==0.2.0
langchain-openai==0.1.7
openai==1.30.1
pydantic==2.7.1
python-dotenv==1.0.1
quadrant==1.0
requests==2.32.0
191 changes: 191 additions & 0 deletions src-agents/phase1/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If not already done run this in the top level folder:\n",
"```\n",
"pip install -r requirements.txt\n",
"```\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import requests\n",
"import os\n",
"import openai\n",
"from enum import Enum\n",
"from pydantic import BaseModel\n",
"from openai import AzureOpenAI\n",
"from dotenv import load_dotenv\n",
"\n",
"# Load environment variables\n",
"if load_dotenv():\n",
" print(\"Found Azure OpenAI API Base Endpoint: \" + os.getenv(\"AZURE_OPENAI_ENDPOINT\"))\n",
"else: \n",
" print(\"Azure OpenAI API Base Endpoint not found. Have you configured the .env file?\")\n",
" \n",
"API_KEY = os.getenv(\"AZURE_OPENAI_API_KEY\")\n",
"API_VERSION = os.getenv(\"OPENAI_API_VERSION\")\n",
"RESOURCE_ENDPOINT = os.getenv(\"AZURE_OPENAI_ENDPOINT\")\n",
"\n",
"\n",
"client = AzureOpenAI(\n",
" azure_endpoint = os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",
" api_key = os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",
" api_version = os.getenv(\"AZURE_OPENAI_VERSION\")\n",
")\n",
"deployment_name = os.getenv(\"AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME\")\n",
"model_name = os.getenv(\"AZURE_OPENAI_COMPLETION_MODEL\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To see if stuff works in general, you can run this."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# response = client.chat.completions.create(\n",
"# model = model_name, \n",
"# messages = [{\"role\" : \"assistant\", \"content\" : \"The one thing I love more than anything else is \"}],\n",
"# )\n",
"\n",
"# print(response)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the object model for receiving questions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"class QuestionType(str, Enum):\n",
" multiple_choice = \"multiple_choice\"\n",
" true_false = \"true_false\"\n",
" popular_choice = \"popular_choice\"\n",
" estimation = \"estimation\"\n",
"\n",
"class Ask(BaseModel):\n",
" question: str | None = None\n",
" type: QuestionType\n",
" correlationToken: str | None = None\n",
"\n",
"class Answer(BaseModel):\n",
" answer: str\n",
" correlationToken: str | None = None\n",
" promptTokensUsed: int | None = None\n",
" completionTokensUsed: int | None = None\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"YOUR Mission: Adjust the function below and reuse it in the main.py file later to deploy to Azure and to update your service\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"async def ask_question(ask: Ask):\n",
" \"\"\"\n",
" Ask a question\n",
" \"\"\"\n",
"\n",
" # Send a completion call to generate an answer\n",
" print('Sending a request to openai')\n",
" start_phrase = ask.question\n",
" response = client.chat.completions.create(\n",
" model = deployment_name,\n",
" messages = [{\"role\" : \"assistant\", \"content\" : start_phrase}],\n",
" )\n",
"\n",
" print(response.choices[0].message.content)\n",
" print(response)\n",
" answer = Answer(answer=response.choices[0].message.content)\n",
" answer.correlationToken = ask.correlationToken\n",
" answer.promptTokensUsed = response.usage.prompt_tokens\n",
" answer.completionTokensUsed = response.usage.completion_tokens\n",
"\n",
" return answer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use this snippet to try your method with several questions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"ask = Ask(question=\"Which of the following is a color? a. Tree b. Flower c. Green\", type=QuestionType.multiple_choice)\n",
"answer = await ask_question(ask)\n",
"print('Answer:', answer)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure you transfer your code changes into main.py (or additional files). Then redeploy your container using this command.\n",
"```\n",
"bash ./azd-hooks/deploy.sh phase1 $AZURE_ENV_NAME\n",
"```\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 09f22ee

Please sign in to comment.