Skip to content

Commit 2c451b1

Browse files
author
tomfrenken
committed
add remaining resilience functioanlity
1 parent fc865a6 commit 2c451b1

File tree

9 files changed

+806
-228
lines changed

9 files changed

+806
-228
lines changed

packages/langchain/README.md

Lines changed: 2 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -56,152 +56,10 @@ Consequently, each deployment ID and resource group uniquely map to a combinatio
5656
## Usage
5757

5858
This package offers both chat and embedding clients, currently supporting Azure OpenAI.
59+
Also supports the SAP Orchestration service.
5960
All clients comply with [LangChain's interface](https://js.langchain.com/docs/introduction).
6061

61-
### Client Initialization
62-
63-
To initialize a client, provide the model name:
64-
65-
```ts
66-
import {
67-
AzureOpenAiChatClient,
68-
AzureOpenAiEmbeddingClient
69-
} from '@sap-ai-sdk/langchain';
70-
71-
// For a chat client
72-
const chatClient = new AzureOpenAiChatClient({ modelName: 'gpt-4o' });
73-
// For an embedding client
74-
const embeddingClient = new AzureOpenAiEmbeddingClient({ modelName: 'gpt-4o' });
75-
```
76-
77-
In addition to the default parameters of the model vendor (e.g., OpenAI) and LangChain, additional parameters can be used to help narrow down the search for the desired model:
78-
79-
```ts
80-
const chatClient = new AzureOpenAiChatClient({
81-
modelName: 'gpt-4o',
82-
modelVersion: '24-07-2021',
83-
resourceGroup: 'my-resource-group'
84-
});
85-
```
86-
87-
**Do not pass a `deployment ID` to initialize the client.**
88-
For the LangChain model clients, initialization is done using the model name, model version and resource group.
89-
90-
An important note is that LangChain clients by default attempt 6 retries with exponential backoff in case of a failure.
91-
Especially in testing environments you might want to reduce this number to speed up the process:
92-
93-
```ts
94-
const embeddingClient = new AzureOpenAiEmbeddingClient({
95-
modelName: 'gpt-4o',
96-
maxRetries: 0
97-
});
98-
```
99-
100-
#### Custom Destination
101-
102-
When initializing the `AzureOpenAiChatClient` and `AzureOpenAiEmbeddingClient` clients, it is possible to provide a custom destination.
103-
For example, when targeting a destination with the name `my-destination`, the following code can be used:
104-
105-
```ts
106-
const chatClient = new AzureOpenAiChatClient(
107-
{
108-
modelName: 'gpt-4o',
109-
modelVersion: '24-07-2021',
110-
resourceGroup: 'my-resource-group'
111-
},
112-
{
113-
destinationName: 'my-destination'
114-
}
115-
);
116-
```
117-
118-
By default, the fetched destination is cached.
119-
To disable caching, set the `useCache` parameter to `false` together with the `destinationName` parameter.
120-
121-
### Chat Client
122-
123-
The chat client allows you to interact with Azure OpenAI chat models, accessible via the generative AI hub of SAP AI Core.
124-
To invoke the client, pass a prompt:
125-
126-
```ts
127-
const response = await chatClient.invoke("What's the capital of France?");
128-
```
129-
130-
#### Advanced Example with Templating and Output Parsing
131-
132-
```ts
133-
import { AzureOpenAiChatClient } from '@sap-ai-sdk/langchain';
134-
import { StringOutputParser } from '@langchain/core/output_parsers';
135-
import { ChatPromptTemplate } from '@langchain/core/prompts';
136-
137-
// initialize the client
138-
const client = new AzureOpenAiChatClient({ modelName: 'gpt-35-turbo' });
139-
140-
// create a prompt template
141-
const promptTemplate = ChatPromptTemplate.fromMessages([
142-
['system', 'Answer the following in {language}:'],
143-
['user', '{text}']
144-
]);
145-
// create an output parser
146-
const parser = new StringOutputParser();
147-
148-
// chain together template, client, and parser
149-
const llmChain = promptTemplate.pipe(client).pipe(parser);
150-
151-
// invoke the chain
152-
return llmChain.invoke({
153-
language: 'german',
154-
text: 'What is the capital of France?'
155-
});
156-
```
157-
158-
### Embedding Client
159-
160-
Embedding clients allow embedding either text or document chunks (represented as arrays of strings).
161-
While you can use them standalone, they are usually used in combination with other LangChain utilities, like a text splitter for preprocessing and a vector store for storage and retrieval of the relevant embeddings.
162-
For a complete example how to implement RAG with our LangChain client, take a look at our [sample code](https://github.com/SAP/ai-sdk-js/blob/main/sample-code/src/langchain-azure-openai.ts).
163-
164-
#### Embed Text
165-
166-
```ts
167-
const embeddedText = await embeddingClient.embedQuery(
168-
'Paris is the capital of France.'
169-
);
170-
```
171-
172-
#### Embed Document Chunks
173-
174-
```ts
175-
const embeddedDocuments = await embeddingClient.embedDocuments([
176-
'Page 1: Paris is the capital of France.',
177-
'Page 2: It is a beautiful city.'
178-
]);
179-
```
180-
181-
#### Preprocess, embed, and store documents
182-
183-
```ts
184-
// Create a text splitter and split the document
185-
const textSplitter = new RecursiveCharacterTextSplitter({
186-
chunkSize: 2000,
187-
chunkOverlap: 200
188-
});
189-
const splits = await textSplitter.splitDocuments(docs);
190-
191-
// Initialize the embedding client
192-
const embeddingClient = new AzureOpenAiEmbeddingClient({
193-
modelName: 'text-embedding-ada-002'
194-
});
195-
196-
// Create a vector store from the document
197-
const vectorStore = await MemoryVectorStore.fromDocuments(
198-
splits,
199-
embeddingClient
200-
);
201-
202-
// Create a retriever for the vector store
203-
const retriever = vectorStore.asRetriever();
204-
```
62+
### SAP Orchestration Service
20563

20664
## Local Testing
20765

packages/langchain/src/README.md

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# @sap-ai-sdk/langchain
2+
3+
SAP Cloud SDK for AI is the official Software Development Kit (SDK) for **SAP AI Core**, **SAP Generative AI Hub**, and **Orchestration Service**.
4+
5+
This package provides LangChain model clients built on top of the foundation model clients of the SAP Cloud SDK for AI.
6+
7+
### Table of Contents
8+
9+
- [Installation](#installation)
10+
- [Prerequisites](#prerequisites)
11+
- [Relationship between Models and Deployment ID](#relationship-between-models-and-deployment-id)
12+
- [Usage](#usage)
13+
- [Client Initialization](#client-initialization)
14+
- [Chat Client](#chat-client)
15+
- [Embedding Client](#embedding-client)
16+
- [Local Testing](#local-testing)
17+
18+
## Installation
19+
20+
```
21+
$ npm install @sap-ai-sdk/langchain
22+
```
23+
24+
## Prerequisites
25+
26+
- [Enable the AI Core service in SAP BTP](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/initial-setup).
27+
- Use the same `@langchain/core` version as the `@sap-ai-sdk/langchain` package, to see which langchain version this package is currently using, check our [package.json](./package.json).
28+
- Configure the project with **Node.js v20 or higher** and **native ESM** support.
29+
- Ensure a deployed OpenAI model is available in the SAP Generative AI Hub.
30+
- Use the [`DeploymentApi`](https://github.com/SAP/ai-sdk-js/blob/main/packages/ai-api/README.md#create-a-deployment) from `@sap-ai-sdk/ai-api` [to deploy a model](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-generative-ai-model-in-sap-ai-core).
31+
Alternatively, you can also create deployments using the [SAP AI Launchpad](https://help.sap.com/docs/sap-ai-core/generative-ai-hub/activate-generative-ai-hub-for-sap-ai-launchpad?locale=en-US&q=launchpad).
32+
- Once deployment is complete, access the model via the `deploymentUrl`.
33+
34+
> **Accessing the AI Core Service via the SDK**
35+
>
36+
> The SDK automatically retrieves the `AI Core` service credentials and resolves the access token needed for authentication.
37+
>
38+
> - In Cloud Foundry, it's accessed from the `VCAP_SERVICES` environment variable.
39+
> - In Kubernetes / Kyma environments, you have to mount the service binding as a secret instead, for more information refer to [this documentation](https://www.npmjs.com/package/@sap/xsenv#usage-in-kubernetes).
40+
41+
## Relationship between Models and Deployment ID
42+
43+
SAP AI Core manages access to generative AI models through the global AI scenario `foundation-models`.
44+
Creating a deployment for a model requires access to this scenario.
45+
46+
Each model, model version, and resource group allows for a one-time deployment.
47+
After deployment completion, the response includes a `deploymentUrl` and an `id`, which is the deployment ID.
48+
For more information, see [here](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-generative-ai-model-in-sap-ai-core).
49+
50+
[Resource groups](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/resource-groups?q=resource+group) represent a virtual collection of related resources within the scope of one SAP AI Core tenant.
51+
52+
Consequently, each deployment ID and resource group uniquely map to a combination of model and model version within the `foundation-models` scenario.
53+
54+
## Usage
55+
56+
This sub-package offers a Azure OpenAI chat and embedding client.
57+
Both clients comply with [LangChain's interface](https://js.langchain.com/docs/introduction).
58+
59+
### Client Initialization
60+
61+
To initialize a client, provide the model name:
62+
63+
```ts
64+
import {
65+
AzureOpenAiChatClient,
66+
AzureOpenAiEmbeddingClient
67+
} from '@sap-ai-sdk/langchain';
68+
69+
// For a chat client
70+
const chatClient = new AzureOpenAiChatClient({ modelName: 'gpt-4o' });
71+
// For an embedding client
72+
const embeddingClient = new AzureOpenAiEmbeddingClient({ modelName: 'gpt-4o' });
73+
```
74+
75+
In addition to the default parameters of the model vendor (e.g., OpenAI) and LangChain, additional parameters can be used to help narrow down the search for the desired model:
76+
77+
```ts
78+
const chatClient = new AzureOpenAiChatClient({
79+
modelName: 'gpt-4o',
80+
modelVersion: '24-07-2021',
81+
resourceGroup: 'my-resource-group'
82+
});
83+
```
84+
85+
**Do not pass a `deployment ID` to initialize the client.**
86+
For the LangChain model clients, initialization is done using the model name, model version and resource group.
87+
88+
An important note is that LangChain clients by default attempt 6 retries with exponential backoff in case of a failure.
89+
Especially in testing environments you might want to reduce this number to speed up the process:
90+
91+
```ts
92+
const embeddingClient = new AzureOpenAiEmbeddingClient({
93+
modelName: 'gpt-4o',
94+
maxRetries: 0
95+
});
96+
```
97+
98+
#### Custom Destination
99+
100+
When initializing the `AzureOpenAiChatClient` and `AzureOpenAiEmbeddingClient` clients, it is possible to provide a custom destination.
101+
For example, when targeting a destination with the name `my-destination`, the following code can be used:
102+
103+
```ts
104+
const chatClient = new AzureOpenAiChatClient(
105+
{
106+
modelName: 'gpt-4o',
107+
modelVersion: '24-07-2021',
108+
resourceGroup: 'my-resource-group'
109+
},
110+
{
111+
destinationName: 'my-destination'
112+
}
113+
);
114+
```
115+
116+
By default, the fetched destination is cached.
117+
To disable caching, set the `useCache` parameter to `false` together with the `destinationName` parameter.
118+
119+
### Chat Client
120+
121+
The chat client allows you to interact with Azure OpenAI chat models, accessible via the generative AI hub of SAP AI Core.
122+
To invoke the client, pass a prompt:
123+
124+
```ts
125+
const response = await chatClient.invoke("What's the capital of France?");
126+
```
127+
128+
#### Advanced Example with Templating and Output Parsing
129+
130+
```ts
131+
import { AzureOpenAiChatClient } from '@sap-ai-sdk/langchain';
132+
import { StringOutputParser } from '@langchain/core/output_parsers';
133+
import { ChatPromptTemplate } from '@langchain/core/prompts';
134+
135+
// initialize the client
136+
const client = new AzureOpenAiChatClient({ modelName: 'gpt-35-turbo' });
137+
138+
// create a prompt template
139+
const promptTemplate = ChatPromptTemplate.fromMessages([
140+
['system', 'Answer the following in {language}:'],
141+
['user', '{text}']
142+
]);
143+
// create an output parser
144+
const parser = new StringOutputParser();
145+
146+
// chain together template, client, and parser
147+
const llmChain = promptTemplate.pipe(client).pipe(parser);
148+
149+
// invoke the chain
150+
return llmChain.invoke({
151+
language: 'german',
152+
text: 'What is the capital of France?'
153+
});
154+
```
155+
156+
### Embedding Client
157+
158+
Embedding clients allow embedding either text or document chunks (represented as arrays of strings).
159+
While you can use them standalone, they are usually used in combination with other LangChain utilities, like a text splitter for preprocessing and a vector store for storage and retrieval of the relevant embeddings.
160+
For a complete example how to implement RAG with our LangChain client, take a look at our [sample code](https://github.com/SAP/ai-sdk-js/blob/main/sample-code/src/langchain-azure-openai.ts).
161+
162+
#### Embed Text
163+
164+
```ts
165+
const embeddedText = await embeddingClient.embedQuery(
166+
'Paris is the capital of France.'
167+
);
168+
```
169+
170+
#### Embed Document Chunks
171+
172+
```ts
173+
const embeddedDocuments = await embeddingClient.embedDocuments([
174+
'Page 1: Paris is the capital of France.',
175+
'Page 2: It is a beautiful city.'
176+
]);
177+
```
178+
179+
#### Preprocess, embed, and store documents
180+
181+
```ts
182+
// Create a text splitter and split the document
183+
const textSplitter = new RecursiveCharacterTextSplitter({
184+
chunkSize: 2000,
185+
chunkOverlap: 200
186+
});
187+
const splits = await textSplitter.splitDocuments(docs);
188+
189+
// Initialize the embedding client
190+
const embeddingClient = new AzureOpenAiEmbeddingClient({
191+
modelName: 'text-embedding-ada-002'
192+
});
193+
194+
// Create a vector store from the document
195+
const vectorStore = await MemoryVectorStore.fromDocuments(
196+
splits,
197+
embeddingClient
198+
);
199+
200+
// Create a retriever for the vector store
201+
const retriever = vectorStore.asRetriever();
202+
```
203+
204+
## Local Testing
205+
206+
For local testing instructions, refer to this [section](https://github.com/SAP/ai-sdk-js/blob/main/README.md#local-testing).

0 commit comments

Comments
 (0)