Skip to content

Commit 530a7e1

Browse files
authored
fix: improved region handling for langchain examples (#1639)
This PR improves the region handling in the `langchain` examples. Before the region was hard-coded to `eu-central-1`, which is not ideal as not everyone is using this region.
1 parent cc4545d commit 530a7e1

File tree

18 files changed

+116
-88
lines changed

18 files changed

+116
-88
lines changed

aws-cs-langserve/.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Program.cs
2+
README.md
3+
venv/

aws-cs-langserve/Aws.Langserve.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
<PackageReference Include="Pulumi.Docker" Version="4.5.1" />
1313
</ItemGroup>
1414

15-
</Project>
15+
</Project>

aws-cs-langserve/Program.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828
var containerContext = config.Get("container-context") ?? ".";
2929
var containerFile = config.Get("container-file") ?? "./Dockerfile";
3030
var openApiKey = config.Get("open-api-key") ?? "CHANGEME";
31-
var availabilityZones = new[]
32-
{
33-
"eu-central-1a",
34-
"eu-central-1b",
35-
};
31+
var region = Aws.GetRegion.Invoke();
3632

3733
var current = Aws.GetCallerIdentity.Invoke();
3834

@@ -137,7 +133,7 @@
137133
{
138134
VpcId = langserveVpc.Id,
139135
CidrBlock = subnet1Cidr,
140-
AvailabilityZone = availabilityZones[0],
136+
AvailabilityZone = region.Apply(getRegionResult => $"{getRegionResult.Name}a"),
141137
MapPublicIpOnLaunch = true,
142138
Tags =
143139
{
@@ -149,7 +145,7 @@
149145
{
150146
VpcId = langserveVpc.Id,
151147
CidrBlock = subnet2Cidr,
152-
AvailabilityZone = availabilityZones[1],
148+
AvailabilityZone = region.Apply(getRegionResult => $"{getRegionResult.Name}b"),
153149
MapPublicIpOnLaunch = true,
154150
Tags =
155151
{
@@ -562,7 +558,7 @@
562558
["options"] = new Dictionary<string, object?>
563559
{
564560
["awslogs-group"] = langserveLogGroupName,
565-
["awslogs-region"] = "eu-central-1",
561+
["awslogs-region"] = region.Apply(getRegionResult => getRegionResult.Name),
566562
["awslogs-stream-prefix"] = "pulumi-langserve",
567563
},
568564
},

aws-cs-langserve/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ To run this example, you'll need the following tools installed on your machine:
1515

1616
## Deploying to AWS using Pulumi
1717

18+
Set the region with the following command:
19+
20+
```bash
21+
pulumi config set aws:region <region>
22+
```
23+
1824
Run the following command to deploy your LangServe app to AWS:
1925

2026
```bash
@@ -27,7 +33,7 @@ pulumi up
2733

2834
This last command will show you a preview of the resources that will be created. After reviewing the changes, you will be prompted to continue. Once confirmed, Pulumi will deploy your LangServe app to AWS.
2935

30-
The whole deployoment process will take a couple of minutes. Once it's done, you will see the URL of your LangServe app in the output.
36+
The whole deployment process will take a couple of minutes. Once it's done, you will see the URL of your LangServe app in the output.
3137

3238
```bash
3339
Outputs:

aws-go-langserve/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ To run this example, you'll need the following tools installed on your machine:
1515

1616
## Deploying to AWS using Pulumi
1717

18+
Set the region with the following command:
19+
20+
```bash
21+
pulumi config set aws:region <region>
22+
```
23+
1824
Run the following command to deploy your LangServe app to AWS:
1925

2026
```bash

aws-go-langserve/main.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ func main() {
6060
if param := cfg.Get("open-api-key"); param != "" {
6161
openApiKey = param
6262
}
63+
region, err := aws.GetRegion(ctx, nil, nil)
64+
if err != nil {
65+
return err
66+
}
6367
availabilityZones := []string{
64-
"eu-central-1a",
65-
"eu-central-1b",
68+
fmt.Sprintf("%va", region.Name),
69+
fmt.Sprintf("%vb", region.Name),
6670
}
6771
current, err := aws.GetCallerIdentity(ctx, nil, nil)
6872
if err != nil {
@@ -562,7 +566,7 @@ func main() {
562566
"logDriver": "awslogs",
563567
"options": map[string]interface{}{
564568
"awslogs-group": langserveLogGroupName,
565-
"awslogs-region": "eu-central-1",
569+
"awslogs-region": region.Name,
566570
"awslogs-stream-prefix": "pulumi-langserve",
567571
},
568572
},

aws-js-langserve/.dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
index.js
2+
package.json
3+
README.md
4+
venv/

aws-js-langserve/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ To run this example, you'll need the following tools installed on your machine:
1414

1515
## Deploying to AWS using Pulumi
1616

17+
Set the region with the following command:
18+
19+
```bash
20+
pulumi config set aws:region <region>
21+
```
22+
1723
Run the following command to deploy your LangServe app to AWS:
1824

1925
```bash

aws-js-langserve/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ const subnet2Cidr = config.get("subnet-2-cidr") || "10.0.1.0/24";
2424
const containerContext = config.get("container-context") || ".";
2525
const containerFile = config.get("container-file") || "./Dockerfile";
2626
const openApiKey = config.get("open-api-key") || "CHANGEME";
27+
const region = aws.getRegion({}).then(region => region.name);
28+
2729
const availabilityZones = [
28-
"eu-central-1a",
29-
"eu-central-1b",
30+
pulumi.interpolate`${region}a`,
31+
pulumi.interpolate`${region}b`,
3032
];
33+
3134
const current = aws.getCallerIdentityOutput({});
3235
const pulumiProject = pulumi.getProject();
3336
const pulumiStack = pulumi.getStack();
@@ -365,7 +368,7 @@ const langserveTaskDefinition = new aws.ecs.TaskDefinition("langserve-task-defin
365368
logDriver: "awslogs",
366369
options: {
367370
"awslogs-group": langserveLogGroupName,
368-
"awslogs-region": "eu-central-1",
371+
"awslogs-region": region,
369372
"awslogs-stream-prefix": "pulumi-langserve",
370373
},
371374
},

aws-py-langserve/.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__main__.py
2+
README.md
3+
venv/

aws-py-langserve/README.md

+33-63
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,59 @@
1-
# AWS Python LangServe Example
1+
# AWS YAML LangServe Example
22

3-
## Installation
3+
This example demonstrates how to use deploy a simple app using Pulumi in YAML.
44

5-
Install the LangChain CLI if you haven't yet
5+
## Prerequisites
66

7-
```bash
8-
pip install -U langchain-cli
9-
```
10-
11-
## Adding packages
7+
To run this example, you'll need the following tools installed on your machine:
128

13-
```bash
14-
# adding packages from
15-
# https://github.com/langchain-ai/langchain/tree/master/templates
16-
langchain app add $PROJECT_NAME
17-
18-
# adding custom GitHub repo packages
19-
langchain app add --repo $OWNER/$REPO
20-
# or with whole git string (supports other git providers):
21-
# langchain app add git+https://github.com/hwchase17/chain-of-verification
9+
1. [Install Pulumi](https://www.pulumi.com/docs/install/)
10+
2. [Install Python](https://www.python.org/downloads/)
11+
2. [Configure AWS](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/)
12+
3. [Install Docker](https://docs.docker.com/get-docker/)
13+
4. [Install the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
14+
5. [Install the LangChain CLI](https://python.langchain.com/docs/langserve#installation)
2215

23-
# with a custom api mount point (defaults to `/{package_name}`)
24-
langchain app add $PROJECT_NAME --api_path=/my/custom/path/rag
25-
```
16+
## Deploying to AWS using Pulumi
2617

27-
Note: you remove packages by their api path
18+
Set the region with the following command:
2819

2920
```bash
30-
langchain app remove my/custom/path/rag
21+
pulumi config set aws:region <region>
3122
```
3223

33-
## Setup LangSmith (Optional)
34-
LangSmith will help us trace, monitor and debug LangChain applications.
35-
LangSmith is currently in private beta, you can sign up [here](https://smith.langchain.com/).
36-
If you don't have access, you can skip this section
37-
38-
39-
```shell
40-
export LANGCHAIN_TRACING_V2=true
41-
export LANGCHAIN_API_KEY=<your-api-key>
42-
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
43-
```
44-
45-
## Launch LangServe
46-
47-
```bash
48-
langchain serve
49-
```
50-
51-
## Deploying to AWS
52-
5324
Run the following command to deploy your LangServe app to AWS:
5425

5526
```bash
27+
git clone https://github.com/pulumi/examples.git
28+
cd examples/aws-py-langserve
29+
pulumi stack init <your-stack-name>
30+
pulumi config set open-api-key --secret # Enter your OpenAI API key
5631
pulumi up
5732
```
5833

59-
This will output the URL of your LangServe app. You can use this URL to make requests to your app.
60-
61-
## Running in Docker
34+
This last command will show you a preview of the resources that will be created. After reviewing the changes, you will be prompted to continue. Once confirmed, Pulumi will deploy your LangServe app to AWS.
6235

63-
This project folder includes a Dockerfile that allows you to easily build and host your LangServe app.
36+
The whole deployoment process will take a couple of minutes. Once it's done, you will see the URL of your LangServe app in the output.
6437

65-
### Building the Image
66-
67-
To build the image, you simply:
38+
```bash
39+
Outputs:
40+
url: "http://<dns>.elb.amazonaws.com"
6841

69-
```shell
70-
docker build . -t my-langserve-app
42+
Resources:
43+
+ 27 created
7144
```
7245

73-
If you tag your image with something other than `my-langserve-app`,
74-
note it for use in the next step.
75-
76-
### Running the Image Locally
46+
You can now access the LangServe playground by adding `/openai/playground` to the URL you got from the output.
7747

78-
To run the image, you'll need to include any environment variables
79-
necessary for your application.
48+
> [!NOTE]
49+
> It may take a few minutes for the load balancer to be ready to accept requests. If you see a 503 error, wait a few minutes and try again.
8050
81-
In the below example, we inject the `OPENAI_API_KEY` environment
82-
variable with the value set in my local environment
83-
(`$OPENAI_API_KEY`)
51+
## Clean up
8452

85-
We also expose port 8080 with the `-p 8080:8080` option.
53+
To clean up the resources created by this example, run the following command:
8654

87-
```shell
88-
docker run -e OPENAI_API_KEY=$OPENAI_API_KEY -p 8080:8080 my-langserve-app
55+
```bash
56+
pulumi destroy
8957
```
58+
59+
You will be prompted to confirm the deletion of the resources. Once confirmed, Pulumi will delete all the resources created by this example.

aws-py-langserve/__main__.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@
3636
open_api_key = config.get("open-api-key")
3737
if open_api_key is None:
3838
open_api_key = "CHANGEME"
39+
40+
region = aws.get_region()
41+
3942
availability_zones = [
40-
"eu-central-1a",
41-
"eu-central-1b",
43+
f"{region.name}a",
44+
f"{region.name}b",
4245
]
4346
current = aws.get_caller_identity_output()
4447
pulumi_project = pulumi.get_project()
@@ -356,7 +359,7 @@
356359
"logDriver": "awslogs",
357360
"options": {
358361
"awslogs-group": args[2],
359-
"awslogs-region": "eu-central-1",
362+
"awslogs-region": region.name,
360363
"awslogs-stream-prefix": "pulumi-langserve",
361364
},
362365
},

aws-ts-langserve/.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
index.ts
2+
README.md
3+
venv/

aws-ts-langserve/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ To run this example, you'll need the following tools installed on your machine:
1515

1616
## Deploying to AWS using Pulumi
1717

18+
Set the region with the following command:
19+
20+
```bash
21+
pulumi config set aws:region <region>
22+
```
23+
1824
Run the following command to deploy your LangServe app to AWS:
1925

2026
```bash

aws-ts-langserve/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ const subnet2Cidr = config.get("subnet-2-cidr") || "10.0.1.0/24";
2323
const containerContext = config.get("container-context") || ".";
2424
const containerFile = config.get("container-file") || "./Dockerfile";
2525
const openApiKey = config.get("open-api-key") || "CHANGEME";
26+
const region = aws.getRegion({}).then(region => region.name);
27+
2628
const availabilityZones = [
27-
"eu-central-1a",
28-
"eu-central-1b",
29+
pulumi.interpolate`${region}a`,
30+
pulumi.interpolate`${region}b`,
2931
];
32+
3033
const current = aws.getCallerIdentityOutput({});
3134
const pulumiProject = pulumi.getProject();
3235
const pulumiStack = pulumi.getStack();
@@ -364,7 +367,7 @@ const langserveTaskDefinition = new aws.ecs.TaskDefinition("langserve-task-defin
364367
logDriver: "awslogs",
365368
options: {
366369
"awslogs-group": langserveLogGroupName,
367-
"awslogs-region": "eu-central-1",
370+
"awslogs-region": region,
368371
"awslogs-stream-prefix": "pulumi-langserve",
369372
},
370373
},

aws-yaml-langserve/.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Pulumi.yaml
2+
README.md

aws-yaml-langserve/Pulumi.yaml

+7-3
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ resources:
324324
logDriver: awslogs
325325
options:
326326
awslogs-group: ${langserve-log-group.name}
327-
awslogs-region: eu-central-1
327+
awslogs-region: ${region.name}
328328
awslogs-stream-prefix: pulumi-langserve
329329
name: ${pulumi-project}-${pulumi-stack}-service
330330
portMappings:
@@ -393,10 +393,14 @@ resources:
393393
Name: ${pulumi-project}-${pulumi-stack}
394394
type: aws:ec2:Vpc
395395
variables:
396+
region:
397+
fn::invoke:
398+
arguments: {}
399+
function: aws:getRegion
396400
accountId: ${current.accountId}
397401
availability-zones:
398-
- eu-central-1a
399-
- eu-central-1b
402+
- ${region.name}a
403+
- ${region.name}b
400404
current:
401405
fn::invoke:
402406
arguments: {}

aws-yaml-langserve/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ To run this example, you'll need the following tools installed on your machine:
1414

1515
## Deploying to AWS using Pulumi
1616

17+
Set the region with the following command:
18+
19+
```bash
20+
pulumi config set aws:region <region>
21+
```
22+
1723
Run the following command to deploy your LangServe app to AWS:
1824

1925
```bash

0 commit comments

Comments
 (0)