Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
denniszielke committed Jun 7, 2024
1 parent 37827fa commit 9562c15
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"dockerfile": "Dockerfile",
"args": {
"VARIANT": "bullseye",
"VERSION": "3.11"
"VERSION": "3.12"
}
},
"features": {
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ curl -X 'POST' \
"correlationToken": "1234567890"
}'
curl -X 'POST' \
"$URL/ask" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"question": "Does The Lost City have any sequels planned? True or False",
"type": "true_or_false",
"correlationToken": "1234567890"
}'
curl -X 'POST' \
"$URL/ask" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"question": "Which country does The Lost Planet take place in?",
"type": "multiple_choice",
"correlationToken": "1234567890"
}'
```

## Deploy resources for Phase 1
Expand All @@ -86,7 +107,7 @@ Run the following script
```
azd env get-values | grep AZURE_ENV_NAME
source <(azd env get-values | grep AZURE_ENV_NAME)
bash ./azd-hooks/deploy.sh phase1 $AZURE_ENV_NAME
bash ./azd-hooks/deploy.sh phase3 $AZURE_ENV_NAME
```

All the other phases work the same.
Expand Down
8 changes: 8 additions & 0 deletions infra/app/phase1.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ module app '../core/host/container-app-upsert.bicep' = {
name: 'OPENAI_API_TYPE'
value: 'azure'
}
{
name: 'AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME'
value: 'text-embedding-ada-002'
}
{
name: 'AZURE_OPENAI_EMBEDDING_MODEL'
value: 'text-embedding-ada-002'
}
]
targetPort: 80
}
Expand Down
8 changes: 8 additions & 0 deletions infra/app/phaseX.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ module app '../core/host/container-app-upsert.bicep' = {
name: 'OPENAI_API_TYPE'
value: 'azure'
}
{
name: 'AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME'
value: 'text-embedding-ada-002'
}
{
name: 'AZURE_OPENAI_EMBEDDING_MODEL'
value: 'text-embedding-ada-002'
}
]
targetPort: 8080
}
Expand Down
6 changes: 0 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
langchain==0.2.0
langchain-community==0.2.1
langchain-core==0.2.3
langchain-openai==0.1.8
openai==1.30.5
pydantic==2.6.4
python-dotenv==1.0.1
tiktoken==0.7.0
markdown==3.6
unstructured==0.14.3
azure-search-documents==11.4.0
azure-identity==1.15.0
uvicorn==0.23.2
Expand Down
2 changes: 1 addition & 1 deletion src-agents/phase1/notebook_p1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.1.-1"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
9 changes: 1 addition & 8 deletions src-agents/phase1/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
langchain==0.2.0
langchain-community==0.2.1
langchain-core==0.2.3
langchain-openai==0.1.8
openai==1.30.5
pydantic==2.6.4
python-dotenv==1.0.1
tiktoken==0.7.0
markdown==3.6
unstructured==0.14.3
azure-search-documents==11.4.0
azure-identity==1.15.0
uvicorn==0.23.2
fastapi==0.110.0
starlette==0.37.2
fastapi==0.110.0
18 changes: 7 additions & 11 deletions src-agents/phase2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from azure.search.documents.models import (
VectorizedQuery
)
from langchain_openai import AzureOpenAIEmbeddings

app = FastAPI()

Expand Down Expand Up @@ -44,14 +43,19 @@ class Answer(BaseModel):
else:
token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")
client = AzureOpenAI(
azure_ad_token_provider=token_provider,
azure_ad_token_provider = token_provider,
api_version = os.getenv("AZURE_OPENAI_VERSION"),
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
)

deployment_name = os.getenv("AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME")
index_name = "movies-semantic-index"
service_endpoint = os.getenv("AZURE_AI_SEARCH_ENDPOINT")
embedding_model = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL")

# use an embeddingsmodel to create embeddings
def get_embedding(text, model=embedding_model):
return client.embeddings.create(input = [text], model=model).data[0].embedding

credential = None
if "AZURE_AI_SEARCH_KEY" in os.environ:
Expand All @@ -65,13 +69,6 @@ class Answer(BaseModel):
credential
)

# use an embeddingsmodel to create embeddings
embeddings_model = AzureOpenAIEmbeddings(
azure_deployment = os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME"),
openai_api_version = os.getenv("OPENAI_EMBEDDING_API_VERSION"),
model= os.getenv("AZURE_OPENAI_EMBEDDING_MODEL")
)

@app.get("/")
async def root():
return {"message": "Hello Smorgs"}
Expand All @@ -81,7 +78,6 @@ async def get_products(query: str = None):
"""
Returns a status of the app
"""

@app.post("/ask", summary="Ask a question", operation_id="ask")
async def ask_question(ask: Ask):
"""
Expand Down Expand Up @@ -114,4 +110,4 @@ async def ask_question(ask: Ask):
answer.promptTokensUsed = response.usage.prompt_tokens
answer.completionTokensUsed = response.usage.completion_tokens

return answer
return answer
39 changes: 23 additions & 16 deletions src-agents/phase2/notebook_p2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
"metadata": {},
"source": [
"## Load custom data\n",
"Here's how you load custom data into an embedding model and how you use langchain to query it.\n"
"Here's how you load custom vector data by means of an embedding model and then query it.\n"
]
},
{
Expand All @@ -170,15 +170,18 @@
"source": [
"import os\n",
"import json\n",
"from langchain_openai import AzureOpenAIEmbeddings\n",
"from openai import AzureOpenAI\n",
"from azure.search.documents import SearchClient\n",
"\n",
"client = AzureOpenAI(\n",
" api_key = os.getenv(\"AZURE_OPENAI_API_KEY\"), \n",
" api_version = os.getenv(\"AZURE_OPENAI_VERSION\"),\n",
" azure_endpoint = os.getenv(\"AZURE_OPENAI_ENDPOINT\")\n",
" )\n",
"\n",
"# use an embeddingsmodel to create embeddings\n",
"embeddings_model = AzureOpenAIEmbeddings( \n",
" azure_deployment = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME\"),\n",
" openai_api_version = os.getenv(\"OPENAI_EMBEDDING_API_VERSION\"),\n",
" model= os.getenv(\"AZURE_OPENAI_EMBEDDING_MODEL\")\n",
")\n",
"def get_embedding(text, model=os.getenv(\"AZURE_OPENAI_EMBEDDING_MODEL\")):\n",
" return client.embeddings.create(input = [text], model=model).data[0].embedding\n",
"\n",
"# 1. define function to parse csv row and create embedding for overview text\n",
"def parseMovie(movie):\n",
Expand All @@ -190,7 +193,7 @@
" (\"year\", str(movie[\"movie_year\"])),\n",
" (\"rating\", str(movie[\"movie_rating\"])),\n",
" (\"plot\", movie[\"movie_plot\"]),\n",
" (\"vector\", embeddings_model.embed_query(movie[\"movie_plot\"]))\n",
" (\"vector\", get_embedding(movie[\"movie_plot\"]))\n",
" ])\n",
"\n",
"# 2. load movies from csv\n",
Expand Down Expand Up @@ -254,7 +257,7 @@
"question = \"Tell me about the latest Ant Man movie. When was it released?\"\n",
"\n",
"# create a vectorized query based on the question\n",
"vector = VectorizedQuery(vector=embeddings_model.embed_query(question), k_nearest_neighbors=5, fields=\"vector\")\n",
"vector = VectorizedQuery(vector=get_embedding(question), k_nearest_neighbors=5, fields=\"vector\")\n",
"\n",
"\n",
"# create search client to retrieve movies from the vector store\n",
Expand Down Expand Up @@ -318,11 +321,15 @@
" \"\"\"\n",
" Ask a question\n",
" \"\"\"\n",
" #####\n",
" # implement rag flow here\n",
" ######\n",
" \n",
" print(answer)\n",
" answer = \"I dont know\"\n",
"\n",
" #####\\n\",\n",
" # implement rag flow here\\n\",\n",
" ######\\n\",\n",
" \n",
" \n",
" print('Answer:', answer)\n",
" return answer"
]
},
Expand All @@ -340,9 +347,9 @@
"outputs": [],
"source": [
"\n",
"ask = Ask(question=\"Tell me about the latest Ant Man movie. When was it released? \", type=QuestionType.estimation)\n",
"ask = Ask(question=\"How many actors were featured in The Smonger Games?\", type=QuestionType.estimation)\n",
"answer = await ask_question(ask)\n",
"print('Answer:', answer)\n"
"print('Answer:', answer)"
]
},
{
Expand Down Expand Up @@ -372,7 +379,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.1.-1"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
6 changes: 0 additions & 6 deletions src-agents/phase2/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
langchain==0.2.0
langchain-community==0.2.1
langchain-core==0.2.3
langchain-openai==0.1.8
openai==1.30.5
pydantic==2.6.4
python-dotenv==1.0.1
tiktoken==0.7.0
markdown==3.6
unstructured==0.14.3
azure-search-documents==11.4.0
azure-identity==1.15.0
uvicorn==0.23.2
Expand Down
14 changes: 7 additions & 7 deletions src-agents/phase3/notebook_p3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@
"source": [
"import requests\n",
"\n",
"smoorghApi = \"http://localhost:5393\"\n",
"smoorghApi = \"https://.northeurope.azurecontainerapps.io/\"\n",
"\n",
"def get_movie_rating(title):\n",
" try:\n",
" response = requests.get(f\"{smoorghApi}/rating/{title}\")\n",
" print('The api response for rating is:', response.text)\n",
" return response.text\n",
"\n",
" \n",
" except:\n",
" return \"Sorry, I couldn't find a rating for that movie.\"\n",
"\n",
Expand All @@ -119,7 +119,7 @@
" return response.text\n",
"\n",
" except:\n",
" return \"Sorry, I couldn't find a actor for that movie.\"\n",
" return \"Sorry, I couldn't find an actor for that movie.\"\n",
" \n",
"def get_movie_location(title):\n",
" try:\n",
Expand All @@ -128,7 +128,7 @@
" return response.text\n",
"\n",
" except:\n",
" return \"Sorry, I couldn't find a actor for that movie.\"\n",
" return \"Sorry, I couldn't find a location for that movie.\"\n",
"\n",
"def get_movie_genre(title):\n",
" try:\n",
Expand All @@ -137,7 +137,7 @@
" return response.text\n",
"\n",
" except:\n",
" return \"Sorry, I couldn't find a actor for that movie.\"\n",
" return \"Sorry, I couldn't find a genre for that movie.\"\n",
"\n",
"print(get_movie_rating(\"The Lost Planet\"))\n",
"\n",
Expand Down Expand Up @@ -228,7 +228,7 @@
" \n",
" question = ask.question\n",
" messages= [{\"role\" : \"assistant\", \"content\" : question}\n",
" , { \"role\" : \"system\", \"content\" : \"Answer the question of the user and use the tools available to you\"}\n",
" , { \"role\" : \"system\", \"content\" : \"Answer the question of the user and use the tools available to you.\"}\n",
" ]\n",
" first_response = client.chat.completions.create(\n",
" model = deployment_name,\n",
Expand Down Expand Up @@ -299,7 +299,7 @@
"outputs": [],
"source": [
"\n",
"ask = Ask(question=\"Which country does 'The Crystal of Power' take place in??\", type=QuestionType.estimation)\n",
"ask = Ask(question=\"Which country does 'The Lost Planet' take place in?\", type=QuestionType.estimation)\n",
"answer = await ask_question(ask)\n",
"print('Answer:', answer )\n"
]
Expand Down
6 changes: 0 additions & 6 deletions src-agents/phase3/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
langchain==0.2.0
langchain-community==0.2.1
langchain-core==0.2.3
langchain-openai==0.1.8
openai==1.30.5
pydantic==2.6.4
python-dotenv==1.0.1
tiktoken==0.7.0
markdown==3.6
unstructured==0.14.3
azure-search-documents==11.4.0
azure-identity==1.15.0
uvicorn==0.23.2
Expand Down

0 comments on commit 9562c15

Please sign in to comment.