Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azure-openai[minor]: update docs and fix openai api usage #4898

Merged
merged 15 commits into from
Mar 30, 2024
Merged
122 changes: 71 additions & 51 deletions docs/core_docs/docs/integrations/llms/azure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,6 @@ LangChain.js supports integration with [Azure OpenAI](https://azure.microsoft.co

You can learn more about Azure OpenAI and its difference with the OpenAI API on [this page](https://learn.microsoft.com/azure/ai-services/openai/overview). If you don't have an Azure account, you can [create a free account](https://azure.microsoft.com/free/) to get started.

## Using the OpenAI SDK

You can also use the `OpenAI` class to call OpenAI models hosted on Azure.

For example, if your Azure instance is hosted under `https://{MY_INSTANCE_NAME}.openai.azure.com/openai/deployments/{DEPLOYMENT_NAME}`, you could initialize your instance like this:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai
```

```typescript
import { OpenAI } from "@langchain/openai";

const model = new OpenAI({
temperature: 0.9,
azureOpenAIApiKey: "YOUR-API-KEY",
azureOpenAIApiVersion: "YOUR-API-VERSION",
azureOpenAIApiInstanceName: "{MY_INSTANCE_NAME}",
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}",
});
const res = await model.invoke(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
```

If your instance is hosted under a domain other than the default `openai.azure.com`, you'll need to use the alternate `AZURE_OPENAI_BASE_PATH` environment variable.
For example, here's how you would connect to the domain `https://westeurope.api.microsoft.com/openai/deployments/{DEPLOYMENT_NAME}`:

```typescript
import { OpenAI } from "@langchain/openai";

const model = new OpenAI({
temperature: 0.9,
azureOpenAIApiKey: "YOUR-API-KEY",
azureOpenAIApiVersion: "YOUR-API-VERSION",
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}",
azureOpenAIBasePath:
"https://westeurope.api.microsoft.com/openai/deployments", // In Node.js defaults to process.env.AZURE_OPENAI_BASE_PATH
});
const res = await model.invoke(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
```

## Using the Azure OpenAI SDK

You'll first need to install the [`@langchain/azure-openai`](https://www.npmjs.com/package/@langchain/azure-openai) package:
Expand Down Expand Up @@ -87,7 +37,21 @@ const model = new AzureOpenAI({
azureOpenAIEndpoint: "<your_endpoint>",
azureOpenAIApiKey: "<your_key>",
azureOpenAIApiDeploymentName: "<your_deployment_name",
modelName: "<your_model>",
});
```

If you're using Azure Managed Identity, you can also pass the credentials directly to the constructor:

```typescript
import { DefaultAzureCredential } from "@azure/identity";
import { AzureOpenAI } from "@langchain/azure-openai";

const credentials = new DefaultAzureCredential();

const model = new AzureOpenAI({
credentials,
azureOpenAIEndpoint: "<your_endpoint>",
azureOpenAIApiDeploymentName: "<your_deployment_name",
});
```

Expand All @@ -113,3 +77,59 @@ import CodeBlock from "@theme/CodeBlock";
import LLMExample from "@examples/llms/azure_openai.ts";

<CodeBlock language="text">{LLMExample}</CodeBlock>

### Chat usage example

import ChatExample from "@examples/llms/azure_openai-chat.ts";

<CodeBlock language="text">{ChatExample}</CodeBlock>

## Using OpenAI SDK

You can also use the `OpenAI` class to call OpenAI models hosted on Azure.

For example, if your Azure instance is hosted under `https://{MY_INSTANCE_NAME}.openai.azure.com/openai/deployments/{DEPLOYMENT_NAME}`, you could initialize your instance like this:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai
```

```typescript
import { OpenAI } from "@langchain/openai";

const model = new OpenAI({
temperature: 0.9,
azureOpenAIApiKey: "YOUR-API-KEY",
azureOpenAIApiVersion: "YOUR-API-VERSION",
azureOpenAIApiInstanceName: "{MY_INSTANCE_NAME}",
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}",
});
const res = await model.invoke(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
```

If your instance is hosted under a domain other than the default `openai.azure.com`, you'll need to use the alternate `AZURE_OPENAI_BASE_PATH` environment variable.
For example, here's how you would connect to the domain `https://westeurope.api.microsoft.com/openai/deployments/{DEPLOYMENT_NAME}`:

```typescript
import { OpenAI } from "@langchain/openai";

const model = new OpenAI({
temperature: 0.9,
azureOpenAIApiKey: "YOUR-API-KEY",
azureOpenAIApiVersion: "YOUR-API-VERSION",
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}",
azureOpenAIBasePath:
"https://westeurope.api.microsoft.com/openai/deployments", // In Node.js defaults to process.env.AZURE_OPENAI_BASE_PATH
});
const res = await model.invoke(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
```
94 changes: 46 additions & 48 deletions docs/core_docs/docs/integrations/text_embedding/azure_openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,14 @@ LangChain.js supports integration with [Azure OpenAI](https://azure.microsoft.co

You can learn more about Azure OpenAI and its difference with the OpenAI API on [this page](https://learn.microsoft.com/azure/ai-services/openai/overview). If you don't have an Azure account, you can [create a free account](https://azure.microsoft.com/free/) to get started.

## Using the OpenAI SDK
## Using Azure OpenAI SDK

The `OpenAIEmbeddings` class can also use the OpenAI API on Azure to generate embeddings for a given text. By default it strips new line characters from the text, as recommended by OpenAI, but you can disable this by passing `stripNewLines: false` to the constructor.

For example, if your Azure instance is hosted under `https://{MY_INSTANCE_NAME}.openai.azure.com/openai/deployments/{DEPLOYMENT_NAME}`, you
could initialize your instance like this:
You'll first need to install the [`@langchain/azure-openai`](https://www.npmjs.com/package/@langchain/azure-openai) package:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai
```

```typescript
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
azureOpenAIApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.AZURE_OPENAI_API_KEY
azureOpenAIApiVersion: "YOUR-API-VERSION", // In Node.js defaults to process.env.AZURE_OPENAI_API_VERSION
azureOpenAIApiInstanceName: "{MY_INSTANCE_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_INSTANCE_NAME
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME
});
```

If you'd like to initialize using environment variable defaults, the `process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME`
will be used first, then `process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME`. This can be useful if you're using these embeddings
with another Azure OpenAI model.

If your instance is hosted under a domain other than the default `openai.azure.com`, you'll need to use the alternate `AZURE_OPENAI_BASE_PATH` environment variable.
For example, here's how you would connect to the domain `https://westeurope.api.microsoft.com/openai/deployments/{DEPLOYMENT_NAME}`:

```typescript
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
azureOpenAIApiKey: "YOUR-API-KEY",
azureOpenAIApiVersion: "YOUR-API-VERSION",
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}",
azureOpenAIBasePath:
"https://westeurope.api.microsoft.com/openai/deployments", // In Node.js defaults to process.env.AZURE_OPENAI_BASE_PATH
});
```

## Using the Azure OpenAI SDK

You'll first need to install the [`@langchain/azure-openai`](https://www.npmjs.com/package/@langchain/azure-openai) package:

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install -S @langchain/azure-openai
```
Expand All @@ -86,7 +43,6 @@ const model = new AzureOpenAI({
azureOpenAIEndpoint: "<your_endpoint>",
azureOpenAIApiKey: "<your_key>",
azureOpenAIApiDeploymentName: "<your_embedding_deployment_name",
modelName: "<your_model>",
});
```

Expand All @@ -102,13 +58,55 @@ const model = new AzureOpenAI({
credentials,
azureOpenAIEndpoint: "<your_endpoint>",
azureOpenAIApiDeploymentName: "<your_embedding_deployment_name",
modelName: "<your_model>",
});
```

### Usage example

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/llms/azure_openai.ts";
import Example from "@examples/embeddings/azure_openai.ts";

<CodeBlock language="text">{Example}</CodeBlock>

## Using OpenAI SDK

The `OpenAIEmbeddings` class can also use the OpenAI API on Azure to generate embeddings for a given text. By default it strips new line characters from the text, as recommended by OpenAI, but you can disable this by passing `stripNewLines: false` to the constructor.

For example, if your Azure instance is hosted under `https://{MY_INSTANCE_NAME}.openai.azure.com/openai/deployments/{DEPLOYMENT_NAME}`, you
could initialize your instance like this:

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai
```

```typescript
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
azureOpenAIApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.AZURE_OPENAI_API_KEY
azureOpenAIApiVersion: "YOUR-API-VERSION", // In Node.js defaults to process.env.AZURE_OPENAI_API_VERSION
azureOpenAIApiInstanceName: "{MY_INSTANCE_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_INSTANCE_NAME
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME
});
```

If you'd like to initialize using environment variable defaults, the `process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME`
will be used first, then `process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME`. This can be useful if you're using these embeddings
with another Azure OpenAI model.

If your instance is hosted under a domain other than the default `openai.azure.com`, you'll need to use the alternate `AZURE_OPENAI_BASE_PATH` environment variable.
For example, here's how you would connect to the domain `https://westeurope.api.microsoft.com/openai/deployments/{DEPLOYMENT_NAME}`:

```typescript
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
azureOpenAIApiKey: "YOUR-API-KEY",
azureOpenAIApiVersion: "YOUR-API-VERSION",
azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}",
azureOpenAIBasePath:
"https://westeurope.api.microsoft.com/openai/deployments", // In Node.js defaults to process.env.AZURE_OPENAI_BASE_PATH
});
```
18 changes: 18 additions & 0 deletions examples/src/llms/azure_openai-chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AzureChatOpenAI } from "@langchain/azure-openai";

export const run = async () => {
const model = new AzureChatOpenAI({
modelName: "gpt-4",
prefixMessages: [
{
role: "system",
content: "You are a helpful assistant that answers in pirate language",
},
],
maxTokens: 50,
});
const res = await model.invoke(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
};
36 changes: 32 additions & 4 deletions libs/langchain-azure-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You can do so by adding appropriate fields to your project's `package.json` like
"name": "your-project",
"version": "0.0.0",
"dependencies": {
"@langchain/openai": "^0.0.9",
"@langchain/azure-openai": "^0.0.4",
"langchain": "0.0.207"
},
"resolutions": {
Expand Down Expand Up @@ -56,7 +56,6 @@ Then initialize the model and make the calls:
import { AzureChatOpenAI } from "@langchain/azure-openai";

const model = new AzureChatOpenAI({
modelName: "gpt-4-1106-preview",
// Note that the following are optional, and will default to the values below
// if not provided.
azureOpenAIEndpoint: process.env.AZURE_OPENAI_API_ENDPOINT,
Expand All @@ -72,7 +71,6 @@ const response = await model.invoke(new HumanMessage("Hello world!"));
import { AzureChatOpenAI } from "@langchain/azure-openai";

const model = new AzureChatOpenAI({
modelName: "gpt-4-1106-preview",
// Note that the following are optional, and will default to the values below
// if not provided.
azureOpenAIEndpoint: process.env.AZURE_OPENAI_API_ENDPOINT,
Expand All @@ -94,11 +92,41 @@ const embeddings = new AzureOpenAIEmbeddings({
// if not provided.
azureOpenAIEndpoint: process.env.AZURE_OPENAI_API_ENDPOINT,
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME,
});
const res = await embeddings.embedQuery("Hello world");
```

## Using Azure managed identity

If you're using [Azure Managed Identity](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity), you can also pass the credentials directly to the constructor:

```typescript
import { DefaultAzureCredential } from "@azure/identity";
import { AzureOpenAI } from "@langchain/azure-openai";

const credentials = new DefaultAzureCredential();

const model = new AzureOpenAI({
credentials,
azureOpenAIEndpoint: process.env.AZURE_OPENAI_API_ENDPOINT,
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
});
```

## Compatibility with OpenAI API

This library is provides compatibility with the OpenAI API. You can use an API key from OpenAI's developer portal like in the example below:

```typescript
import { AzureOpenAI, OpenAIKeyCredential } from "@langchain/azure-openai";

const model = new AzureOpenAI({
modelName: "gpt-3.5-turbo",
credentials: new OpenAIKeyCredential("<your_openai_api_key>"),
});
```

## Development

To develop the Azure OpenAI package, you'll need to follow these instructions:
Expand Down
Loading
Loading