{"id":13289027,"url":"https://github.com/Azure-Samples/function-python-ai-langchain","last_synced_at":"2025-03-10T06:33:55.347Z","repository":{"id":168828222,"uuid":"644532832","full_name":"Azure-Samples/function-python-ai-langchain","owner":"Azure-Samples","description":"Simple starting point function to host LangChains with LLMs and other tools in an Azure Function.","archived":false,"fork":false,"pushed_at":"2024-10-21T14:26:32.000Z","size":63,"stargazers_count":56,"open_issues_count":0,"forks_count":25,"subscribers_count":12,"default_branch":"main","last_synced_at":"2024-10-21T23:54:37.578Z","etag":null,"topics":["azd","azd-templates","azure","azure-functions","chatgpt","langchain","openai","python"],"latest_commit_sha":null,"homepage":"","language":"Bicep","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Azure-Samples.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-23T18:05:39.000Z","updated_at":"2024-10-21T14:27:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"b70718c9-e636-4cf0-94f7-ed0ff769f1e5","html_url":"https://github.com/Azure-Samples/function-python-ai-langchain","commit_stats":null,"previous_names":["azure-samples/function-python-ai-langchain"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-langchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-langchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-langchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-langchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Azure-Samples","download_url":"https://codeload.github.com/Azure-Samples/function-python-ai-langchain/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242805643,"owners_count":20187996,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azd","azd-templates","azure","azure-functions","chatgpt","langchain","openai","python"],"created_at":"2024-07-29T17:00:18.141Z","updated_at":"2025-03-10T06:33:54.841Z","avatar_url":"https://github.com/Azure-Samples.png","language":"Bicep","funding_links":[],"categories":["Building"],"sub_categories":["Tools"],"readme":"---\npage_type: sample\nlanguages:\n- azdeveloper\n- python\n- bicep\nproducts:\n- azure\n- azure-functions\n- azure-openai\nurlFragment: function-python-ai-langchain\nname: Azure Functions - LangChain with Azure OpenAI and ChatGPT (Python v2 Function)\ndescription: Using human prompt with Python as HTTP Get or Post input, calculates the completions using chains of human input and templates. \n---\n\u003c!-- YAML front-matter schema: https://review.learn.microsoft.com/en-us/help/contribute/samples/process/onboarding?branch=main#supported-metadata-fields-for-readmemd --\u003e\n\n# Azure Functions\n## LangChain with Azure OpenAI and ChatGPT (Python v2 Function)\n\nThis sample shows how to take a human prompt as HTTP Get or Post input, calculates the completions using chains of human input and templates.  This is a starting point that can be used for more sophisticated chains.  \n\n[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://github.com/codespaces/new?hide_repo_select=true\u0026ref=main\u0026repo=575770869)\n\n## Run on your local environment\n\n### Pre-reqs\n1) [Python 3.8+](https://www.python.org/) \n2) [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)\n3) [Azure Developer CLI](https://aka.ms/azd)\n4) Once you have your Azure subscription, run the following in a new terminal window to create Azure OpenAI and other resources needed:\n```bash\nazd provision\n```\n\nTake note of the value of `AZURE_OPENAI_ENDPOINT` which can be found in `./.azure/\u003cenv name from azd provision\u003e/.env`.  It will look something like:\n```bash\nAZURE_OPENAI_ENDPOINT=\"https://cog-\u003cunique string\u003e.openai.azure.com/\"\n```\n\n5) 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.  \n```json\n{\n  \"IsEncrypted\": false,\n  \"Values\": {\n    \"FUNCTIONS_WORKER_RUNTIME\": \"python\",\n    \"AzureWebJobsFeatureFlags\": \"EnableWorkerIndexing\",\n    \"AzureWebJobsStorage\": \"UseDevelopmentStorage=true\",\n    \"AZURE_OPENAI_ENDPOINT\": \"https://\u003cyour deployment\u003e.openai.azure.com/\",\n    \"AZURE_OPENAI_CHATGPT_DEPLOYMENT\": \"chat\",\n    \"OPENAI_API_VERSION\": \"2023-05-15\"\n  }\n}\n```\n\n### Using Functions CLI\n1) Open a new terminal and do the following:\n\n```bash\npip3 install -r requirements.txt\nfunc start\n```\n2) Using your favorite REST client, e.g. [RestClient in VS Code](https://marketplace.visualstudio.com/items?itemName=humao.rest-client), PostMan, curl, make a post.  `test.http` has been provided to run this quickly.   \n\nTerminal:\n```bash\ncurl -i -X POST http://localhost:7071/api/ask/ \\\n  -H \"Content-Type: text/json\" \\\n  --data-binary \"@testdata.json\"\n```\n\n`testdata.json`\n```json\n{\n    \"prompt\": \"What is a good feature of Azure Functions?\"\n}\n```\n\n`test.http`\n```bash\n\nPOST http://localhost:7071/api/ask HTTP/1.1\ncontent-type: application/json\n\n{\n    \"prompt\": \"What is a good feature of Azure Functions?\"\n}\n```\n\n### Using Visual Studio Code\n1) Open this repo in VS Code:\n```bash\ncode .\n```\n\n2) Follow the prompts to load Function.  It is recommended to Initialize the Functions Project for VS Code, and also to enable a virtual environment for your chosen version of Python.  \n\n3) Run and Debug `F5` the app\n\n4) Test using same REST client steps above\n\n## Deploy to Azure\n\nThe easiest way to deploy this app is using the [Azure Dev CLI](https://aka.ms/azd).  If you open this repo in GitHub CodeSpaces the AZD tooling is already preinstalled.\n\nTo provision and deploy:\n```bash\nazd up\n```\n\n## Source Code\n\nThe 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 via `init()` function, we can leverage favorite aspects of LangChain in the `main()` (ask) function.  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 and other aspects of Langchain's breadth of features.    \n\n```python\nllm = AzureChatOpenAI(\n    deployment_name=AZURE_OPENAI_CHATGPT_DEPLOYMENT,\n    temperature=0.3\n    )\nllm_prompt = PromptTemplate.from_template(\n    \"The following is a conversation with an AI assistant. \" +\n    \"The assistant is helpful.\\n\\n\" +\n    \"A:How can I help you today?\\n\" +\n    \"Human: {human_prompt}?\"\n    )\nformatted_prompt = llm_prompt.format(human_prompt=prompt)\n\nresponse = llm.invoke(formatted_prompt)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Ffunction-python-ai-langchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAzure-Samples%2Ffunction-python-ai-langchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Ffunction-python-ai-langchain/lists"}