Code Monkey home page Code Monkey logo

demo-langchain-apim's Introduction

demo-langchain-apim

Demo calling Azure OpenAI with LangChain via Azure API Management (APIM).

Update: Now includes an example using the OpenAI SDK: apim-aoai-sdk.py

Azure Environment

Your APIM will need to have Azure OpenAI REST API methods implemented (import OpenAPI spec from docs).

On your API, rename the subscription key header paramenter to api-key. Create an APIM subscription with access to the APIs.

APIM API Configuration

Note that "Open AI" endpoints are expected to end with /openai after the endpoint base URL (AZURE_OPENAI_ENDPOINT). When configuring your API in APIM Management, set the API URL Suffix to end with /openai, either just by setting it to openai or something-else/openai.

API Configuration in APIM

Running the Sample

To run this sample, rename .env.sample to .env and populate the values for:

  • AZURE_OPENAI_API_KEY: Your APIM subscription key
  • AZURE_OPENAI_ENDPOINT: The URL to your APIM OpenAI API

Important

The AZURE_OPENAI_ENDPOINT value should be the URL up to /openai but not include the /openai part of the URL.

Example: If your API URL is something like: https://my-apim.azure-api.net/openai/deployments/...
... then your AZURE_OPENAPI_ENDPOINT should be: https://my-apim.azure-api.net

Samples Available

  • apim.py: Calling the chat completions API from LangChain
  • apim-aoai-sdk.py: Calling using the chat completions API with the OpenAI SDK
  • apim.http: Calling the chat completions API directly with HTTP
    • Here you can see the X-MS-Region response header which indicates the Azure region used by Azure OpenAI.
    • Uses the VS Code REST Client extension.

Other Resources

Example of APIM policy to load balance AOAI across multiple regions/deployments: https://gist.github.com/ChrisRomp/f038cc233d99eaf578065723bade2a26

Load Balancing AOAI via APIM: https://github.com/ian-t-adams/azure-openai-api-m-retry -- This example walks through configuring APIM in Azure as well.

Active/Passive load balancing with APIM: https://techcommunity.microsoft.com/t5/fasttrack-for-azure/smart-load-balancing-for-openai-endpoints-and-azure-api/ba-p/3991616

Troubleshooting

The Python debugger should provide reasonable output to diagnose issues, however you may also wish to enable diagnostic logging in Azure API Management and configure it to emit Gateway logs to Azure Log Analytics or another option.

In Log Analytics, you can see APIM Gateway log messages including errors for bad requests using a query like:

ApiManagementGatewayLogs
| order by TimeGenerated desc

Or filter to failed requests with:

ApiManagementGatewayLogs
| where not(IsRequestSuccess)
| order by TimeGenerated desc

demo-langchain-apim's People

Contributors

dependabot[bot] avatar chrisromp avatar

Stargazers

 avatar  avatar Cameron King avatar

Watchers

 avatar Kostas Georgiou avatar

demo-langchain-apim's Issues

endpoint doesn't end with "openai" suffix

Hi,

This is a very helpful repo, thanks for creating this. I have one doubt that - is there any fix to it that I'd be able to do if my apim endpoint doesn't have "openai" suffix ? Any guidance you can provide regarding this would be very helpful.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.