Skip to content

Commit 0a352fa

Browse files
authoredOct 15, 2024
Merge pull request #10 from paulyuk/main
Modernized for updated Langchain, Flex Consumption and MI
2 parents 7c091bb + 4251003 commit 0a352fa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+899
-2240
lines changed
 

‎README.md

+36-10
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,28 @@ This sample shows how to take a human prompt as HTTP Get or Post input, calculat
2626
### Pre-reqs
2727
1) [Python 3.8+](https://www.python.org/)
2828
2) [Azure Functions Core Tools](https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=v4%2Cmacos%2Ccsharp%2Cportal%2Cbash#install-the-azure-functions-core-tools)
29-
3) [Azure OpenAPI API key, endpoint, and deployment](https://portal.azure.com)
30-
4) Add this `local.settings.json` file to this folder to simplify local development and include Key from step 3
29+
3) [Azure Developer CLI](https://aka.ms/azd)
30+
4) Once you have your Azure subscription, run the following in a new terminal window to create Azure OpenAI and other resources needed:
31+
```bash
32+
azd provision
33+
```
34+
35+
Take note of the value of `AZURE_OPENAI_ENDPOINT` which can be found in `./.azure/<env name from azd provision>/.env`. It will look something like:
36+
```bash
37+
AZURE_OPENAI_ENDPOINT="https://cog-<unique string>.openai.azure.com/"
38+
```
3139

32-
`./local.settings.json`
40+
5) Add this `local.settings.json` file to the root of the repo folder to simplify local development. Replace `AZURE_OPENAI_ENDPOINT` with your value from step 4. Optionally you can choose a different model deployment in `AZURE_OPENAI_CHATGPT_DEPLOYMENT`. This file will be gitignored to protect secrets from committing to your repo, however by default the sample uses Entra identity (user identity and mananaged identity) so it is secretless.
3341
```json
3442
{
3543
"IsEncrypted": false,
3644
"Values": {
3745
"FUNCTIONS_WORKER_RUNTIME": "python",
3846
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
39-
"AzureWebJobsStorage": "",
40-
"AZURE_OPENAI_KEY": "...",
41-
"AZURE_OPENAI_ENDPOINT": "https://<service_name>.openai.azure.com/",
42-
"AZURE_OPENAI_SERVICE": "...",
43-
"AZURE_OPENAI_CHATGPT_DEPLOYMENT": "...",
44-
"OPENAI_API_VERSION": "2023-05-15",
45-
"USE_LANGCHAIN": "True"
47+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
48+
"AZURE_OPENAI_ENDPOINT": "https://<your deployment>.openai.azure.com/",
49+
"AZURE_OPENAI_CHATGPT_DEPLOYMENT": "chat",
50+
"OPENAI_API_VERSION": "2023-05-15"
4651
}
4752
}
4853
```
@@ -120,3 +125,24 @@ To provision and deploy:
120125
```bash
121126
azd up
122127
```
128+
129+
## Source Code
130+
131+
The key code that makes the prompting and completion work is as follows in [function_app.py](function_app.py). The `/api/ask` function and route expects a prompt to come in the POST body using a standard HTTP Trigger in Python. Then once the environment variables are set to configure OpenAI and LangChain frameworks, we can leverage favorite aspects of LangChain. In this simple example we take a prompt, build a better prompt from a template, and then invoke the LLM. By default the LLM deployment is `gpt-35-turbo` as defined in [./infra/main.parameters.json](./infra/main.parameters.json) but you can experiment with other models.
132+
133+
```python
134+
llm = AzureChatOpenAI(
135+
deployment_name=AZURE_OPENAI_CHATGPT_DEPLOYMENT,
136+
temperature=0.3,
137+
openai_api_key=AZURE_OPENAI_KEY
138+
)
139+
llm_prompt = PromptTemplate.from_template(
140+
"The following is a conversation with an AI assistant. " +
141+
"The assistant is helpful.\n\n" +
142+
"A:How can I help you today?\nHuman: {human_prompt}?"
143+
)
144+
formatted_prompt = llm_prompt.format(human_prompt=prompt)
145+
146+
response = llm.invoke(formatted_prompt)
147+
logging.info(response.content)
148+
```

‎azure.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name: langchain-py-ai-func
44
metadata:
5-
template: langchain-py-ai-func@0.0.1-beta
5+
template: langchain-py-ai-func@1.0.0
66
services:
77
api:
88
project: ./

0 commit comments

Comments
 (0)