Skip to content

Commit 873f4d9

Browse files
author
tomfrenken
committed
add more docs
1 parent b3bce6d commit 873f4d9

File tree

8 files changed

+89
-97
lines changed

8 files changed

+89
-97
lines changed

packages/langchain/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This package provides LangChain model clients built on top of the foundation mod
1010
- [Prerequisites](#prerequisites)
1111
- [Relationship between Models and Deployment ID](#relationship-between-models-and-deployment-id)
1212
- [Usage](#usage)
13-
- [SAP Orchestration Service](#sap-orchestration-service)
14-
- [Azure OpenAI](#azure-openai)
13+
- [SAP Orchestration Service](#sap-orchestration-service)
14+
- [Azure OpenAI](#azure-openai)
1515
- [Local Testing](#local-testing)
1616
- [Support, Feedback, Contribution](#support-feedback-contribution)
1717
- [License](#license)

packages/langchain/src/openai/README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ This package provides LangChain model clients built on top of the foundation mod
1414
- [Chat Client](#chat-client)
1515
- [Embedding Client](#embedding-client)
1616
- [Local Testing](#local-testing)
17-
- [Support, Feedback, Contribution](#support-feedback-contribution)
18-
- [License](#license)
1917

2018
## Installation
2119

@@ -206,14 +204,3 @@ const retriever = vectorStore.asRetriever();
206204
## Local Testing
207205

208206
For local testing instructions, refer to this [section](https://github.com/SAP/ai-sdk-js/blob/main/README.md#local-testing).
209-
210-
## Support, Feedback, Contribution
211-
212-
This project is open to feature requests, bug reports and questions via [GitHub issues](https://github.com/SAP/ai-sdk-js/issues).
213-
214-
Contribution and feedback are encouraged and always welcome.
215-
For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](https://github.com/SAP/ai-sdk-js/blob/main/CONTRIBUTING.md).
216-
217-
## License
218-
219-
The SAP Cloud SDK for AI is released under the [Apache License Version 2.0.](http://www.apache.org/licenses/).

packages/langchain/src/orchestration/README.md

Lines changed: 57 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ This package provides LangChain model clients built on top of the foundation mod
1111
- [Relationship between Models and Deployment ID](#relationship-between-models-and-deployment-id)
1212
- [Usage](#usage)
1313
- [Client Initialization](#client-initialization)
14-
- [Chat Client](#chat-client)
15-
- [Embedding Client](#embedding-client)
14+
- [Client](#client)
15+
- [Resilience](#resilience)
1616
- [Local Testing](#local-testing)
17-
- [Support, Feedback, Contribution](#support-feedback-contribution)
18-
- [License](#license)
17+
- [Limitations](#limitations)
1918

2019
## Installation
2120

@@ -55,60 +54,46 @@ Consequently, each deployment ID and resource group uniquely map to a combinatio
5554

5655
## Usage
5756

58-
This package offers both chat and embedding clients, currently supporting Azure OpenAI.
59-
All clients comply with [LangChain's interface](https://js.langchain.com/docs/introduction).
57+
This package offers a LangChain orchestration service client.
58+
It does not support streaming yet.
59+
The client complies with the [LangChain's interface](https://js.langchain.com/docs/introduction).
6060

6161
### Client Initialization
6262

63-
To initialize a client, provide the model name:
63+
To initialize the client, you can provide 4 different configurations.
64+
The only required one is the [orchestration configuration](), however, you can also set:
6465

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-
```
66+
- public langchainOptions: BaseChatModelParams = {},
67+
public deploymentConfig?: ResourceGroupConfig,
68+
public destination?: HttpDestinationOrFetchOptions
7669

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:
70+
A minimal example to instantiate the orchestration client uses a template and model name:
7871

7972
```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:
73+
const config: OrchestrationModuleConfig = {
74+
llm: {
75+
model_name: 'gpt-35-turbo'
76+
},
77+
templating: {
78+
template: [
79+
{ role: 'user', content: 'Give me a long introduction of {{?subject}}' }
80+
]
81+
}
82+
};
9283

93-
```ts
94-
const embeddingClient = new OrchestrationClient({
95-
modelName: 'gpt-4o',
96-
maxRetries: 0
97-
});
84+
const client = new OrchestratioClient(config);
9885
```
9986

10087
#### Custom Destination
10188

102-
When initializing the `AzureOpenAiChatClient` and `AzureOpenAiEmbeddingClient` clients, it is possible to provide a custom destination.
89+
When initializing the `OrchestrationClient`, it is possible to provide a custom destination.
10390
For example, when targeting a destination with the name `my-destination`, the following code can be used:
10491

10592
```ts
106-
const chatClient = new AzureOpenAiChatClient(
107-
{
108-
modelName: 'gpt-4o',
109-
modelVersion: '24-07-2021',
110-
resourceGroup: 'my-resource-group'
111-
},
93+
const client = new OrchestrationClient(
94+
orchestrationConfig,
95+
langchainOptions,
96+
deploymentConfig,
11297
{
11398
destinationName: 'my-destination'
11499
}
@@ -118,54 +103,48 @@ const chatClient = new AzureOpenAiChatClient(
118103
By default, the fetched destination is cached.
119104
To disable caching, set the `useCache` parameter to `false` together with the `destinationName` parameter.
120105

121-
### Chat Client
106+
### Client Invocation
122107

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:
108+
When invoking the client, you only have to pass a message history and most of the time input parameters for the template module.
125109

126110
```ts
127-
const response = await chatClient.invoke("What's the capital of France?");
111+
const systemMessage = new SystemMessage('Be a helpful assisstant!');
112+
const history = [systemMessage];
113+
const response = await client.invoke(history, {
114+
inputParams: { subject: 'paris' }
115+
});
116+
```
117+
118+
#### Resilience
119+
120+
If you need to add resilience to your client, you can make use of the default options available in LangChain, most importantly `timeout` and `maxRetry`.
121+
122+
##### Timeout
123+
124+
By default, there is no timeout set in the client, if you want to limit the maximum duration the entire request, including retries, should take,
125+
you can set a timeout duration in ms when using the invoke method:
126+
127+
```ts
128+
const response = await client.invoke(messageHistory, { timeout: 10000 });
128129
```
129130

130-
#### Advanced Example with Templating and Output Parsing
131+
##### Retry
132+
133+
By default, LangChain clients retry 6 times, if you want to adjust this behavior, you need to do so at client initialization:
131134

132135
```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?'
136+
const client = new OrchestrationClient(orchestrationConfig, {
137+
maxRetries: 0
155138
});
156139
```
157140

158141
## Local Testing
159142

160143
For local testing instructions, refer to this [section](https://github.com/SAP/ai-sdk-js/blob/main/README.md#local-testing).
161144

162-
## Support, Feedback, Contribution
163-
164-
This project is open to feature requests, bug reports and questions via [GitHub issues](https://github.com/SAP/ai-sdk-js/issues).
165-
166-
Contribution and feedback are encouraged and always welcome.
167-
For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](https://github.com/SAP/ai-sdk-js/blob/main/CONTRIBUTING.md).
145+
## Limitations
168146

169-
## License
147+
Currently unsupported features are:
170148

171-
The SAP Cloud SDK for AI is released under the [Apache License Version 2.0.](http://www.apache.org/licenses/).
149+
- Streaming
150+
- Tool Calling

packages/langchain/src/orchestration/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ import type { CustomRequestConfig } from '@sap-ai-sdk/core';
77
*/
88
export type OrchestrationCallOptions = Pick<
99
BaseChatModelCallOptions,
10-
'stop' | 'signal' | 'timeout' | 'callbacks' | 'metadata' | 'runId' | 'runName' | 'tags'
10+
| 'stop'
11+
| 'signal'
12+
| 'timeout'
13+
| 'callbacks'
14+
| 'metadata'
15+
| 'runId'
16+
| 'runName'
17+
| 'tags'
1118
> & {
1219
customRequestConfig?: CustomRequestConfig;
1320
tools?: Template['tools'];

packages/langchain/src/orchestration/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function mapHumanMessageToChatMessage(message: HumanMessage): ChatMessage {
7979
function mapSystemMessageToAzureOpenAiSystemMessage(
8080
message: SystemMessage
8181
): ChatMessage {
82-
// TODO: Remove as soon as image_url is a supported inputed for system messages in orchestration.
82+
// TODO: Remove as soon as image_url is a supported input for system messages in orchestration.
8383
if (
8484
typeof message.content !== 'string' &&
8585
message.content.some(content => content.type === 'image_url')

sample-code/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,21 @@ Once the streaming is done, finish reason and token usage are printed out.
212212

213213
Invoke LangChain Azure OpenAI client with a simple input to get chat completion response.
214214

215-
#### Invoke a Chain for Templating
215+
#### Invoke a Chain with Templating
216216

217217
`GET /langchain/invoke-chain`
218218

219219
Invoke chain to get chat completion response from Azure OpenAI.
220220
The chain contains a template and a string parser.
221221

222-
#### Invoke a Chain for Retrieval-Augmented Generation (RAG)
222+
#### Invoke a Chain with Templating and Orchestration Client
223+
224+
`GET /langchain/invoke-chain-orchestration`
225+
226+
Invoke a chain to get a orchestration response from the orchestration service.
227+
The chain has a built-in template and is chained with a string parser.
228+
229+
#### Invoke a Chain with Retrieval-Augmented Generation (RAG)
223230

224231
`GET /langchain/invoke-rag-chain`
225232

sample-code/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export {
2828
invokeChain,
2929
invokeRagChain
3030
} from './langchain-azure-openai.js';
31+
export { invokeChain as orchestrationInvokeChain } from './langchain-orchestration.js';
3132
export {
3233
getDeployments,
3334
getDeploymentsWithDestination,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { orchestrationInvokeChain } from '@sap-ai-sdk/sample-code';
2+
import { loadEnv } from './utils/load-env.js';
3+
4+
loadEnv();
5+
6+
describe('Langchain OpenAI Access', () => {
7+
it('executes invoke as part of a chain ', async () => {
8+
const result = await orchestrationInvokeChain();
9+
expect(result).toContain('SAP Cloud SDK');
10+
});
11+
});

0 commit comments

Comments
 (0)