blob: 62ebe5fbe35ef70bf28bf3d7befc5f6af5830531 [file] [log] [blame]
== AWS Bedrock Knowledge Base example
In this sample you'll use the AWS Bedrock component.
Through the usage of EventBridge and SQS Services you'll be able to consume events from specific bucket and start ingestion job on your knowledge base.
=== Install JBang
First install JBang according to https://www.jbang.dev
When JBang is installed then you should be able to run from a shell:
[source,sh]
----
$ jbang --version
----
This will output the version of JBang.
To run this example you can either install Camel on JBang via:
[source,sh]
----
$ jbang app install camel@apache/camel
----
Which allows to run CamelJBang with `camel` as shown below.
=== Setup the AWS S3 bucket, SQS Queue and EventBridge Rule through Terraform
If you are in a hurry you can also try this example by running the Terraform configuration provided in Terraform folder.
[source,sh]
----
cd terraform/
----
and then run
[source,sh]
----
terraform init
----
At this point you should be to run the configuration
[source,sh]
----
terraform apply -var="s3_bucket_name=s3-bedrock-test-123" -var="sqs_queue_name=sqs-bedrock-test-123"
----
You can specify whatever bucket name or SQS name you want.
At the end the AWS environment on your account will be completed, and you could go ahead with the example.
Now you should be able to create a knowledge base on your AWS Bedrock console by pointing to the just created S3 bucket.
Don't forget to specify the correct sqs ARN in the application.properties and add the correct knowledge base Id and data source Id for AWS Bedrock.
=== How to run
Then you can run this example using:
[source,sh]
----
$ jbang -Dcamel.jbang.version=4.6.0-SNAPSHOT camel@apache/camel run --properties=application.properties Counter.java BedrockPrompt.java
----
Or run it even shorter:
[source,sh]
----
$ jbang -Dcamel.jbang.version=4.6.0-SNAPSHOT camel@apache/camel run --properties=application.properties *
----
Once started you should see the following log
[source,sh]
----
2024-04-03 10:01:19.794 INFO 23749 --- [ main] el.impl.engine.AbstractCamelContext : Apache Camel 4.6.0-SNAPSHOT (Counter) is starting
2024-04-03 10:01:19.984 INFO 23749 --- [ main] .core.spi.resolver.ResolverProvider : Using the default address resolver as the dns resolver could not be loaded
2024-04-03 10:01:20.110 INFO 23749 --- [ntloop-thread-0] .http.vertx.VertxPlatformHttpServer : Vert.x HttpServer started on 0.0.0.0:8080
2024-04-03 10:01:20.231 INFO 23749 --- [ main] g.apache.camel.main.BaseMainSupport : Property-placeholders summary
2024-04-03 10:01:20.231 INFO 23749 --- [ main] g.apache.camel.main.BaseMainSupport : [application.properties] knowledgeBaseId=xxxx
2024-04-03 10:01:20.231 INFO 23749 --- [ main] g.apache.camel.main.BaseMainSupport : [application.properties] dataSourceId=xxxx
2024-04-03 10:01:20.232 INFO 23749 --- [ main] g.apache.camel.main.BaseMainSupport : [application.properties] prompt=What is the average price of natural gas between 2010 and 2011?
2024-04-03 10:01:20.262 INFO 23749 --- [ main] el.impl.engine.AbstractCamelContext : Routes startup (total:3 rest-dsl:2)
2024-04-03 10:01:20.263 INFO 23749 --- [ main] el.impl.engine.AbstractCamelContext : Started route1 (aws2-sqs://arn:arnsqs)
2024-04-03 10:01:20.263 INFO 23749 --- [ main] el.impl.engine.AbstractCamelContext : Started route2 (rest://get:/bedrock:/prompt)
2024-04-03 10:01:20.263 INFO 23749 --- [ main] el.impl.engine.AbstractCamelContext : Started route3 (rest://get:/bedrock:/ingestion_status/%7Bid%7D)
2024-04-03 10:01:20.263 INFO 23749 --- [ main] el.impl.engine.AbstractCamelContext : Apache Camel 4.6.0-SNAPSHOT (Counter) started in 469ms (build:0ms init:0ms start:469ms)
2024-04-03 10:01:20.266 INFO 23749 --- [ main] t.platform.http.main.MainHttpServer : HTTP endpoints summary
2024-04-03 10:01:20.268 INFO 23749 --- [ main] t.platform.http.main.MainHttpServer : http://0.0.0.0:8080/bedrock/ingestion_status/{id} (GET)
2024-04-03 10:01:20.268 INFO 23749 --- [ main] t.platform.http.main.MainHttpServer : http://0.0.0.0:8080/bedrock/prompt (GET)
----
=== Developer Web Console
You can enable the developer console via `--console` flag as show:
[source,sh]
----
$ jbang -Dcamel.jbang.version=4.6.0-SNAPSHOT camel@apache/camel run --properties=application.properties Counter.java BedrockPrompt.java --console
----
Then you can browse: http://localhost:8080/q/dev to introspect the running Camel Application.
=== Invoke the model
You should now try to submit the prompt through the prompt endpoint:
[source,sh]
----
$ curl http://0.0.0.0:8080/bedrock/prompt/
----
And the answer will be the following:
[source,sh]
----
$ Sorry, I am unable to assist you with this request.
----
=== Get the training data set and use it
Download the required data set from the following link: https://www.kaggle.com/datasets/unibahmad/natural-gas-prices/data
You'll download a file named 'archive.zip'
Unzip it somewhere in your file system and then run the following command
[source,sh]
----
$ aws s3 cp <path_to_training_data>/ s3://<s3-bucket-name> --recursive
----
=== Create and delete an object
Once the PutObject event will be received and all the files will be uploaded, in the integration log you should see:
[source,sh]
----
2024-04-03 10:10:11.851 INFO 24832 --- [edrock-test-123] BedrockPrompt.java:42 : Detected file upload in AWS S3. Starting ingestion process to AWS Bedrock Knowledge Base.
2024-04-03 10:10:13.338 INFO 24832 --- [edrock-test-123] BedrockPrompt.java:46 : The Ingestion Job Id is QQ9OGSZOPD
----
Now check the ingestion job status
[source,sh]
----
curl http://0.0.0.0:8080/bedrock/ingestion_status/QQ9OGSZOPD
IN_PROGRESS
.
.
.
.
curl http://0.0.0.0:8080/bedrock/ingestion_status/QQ9OGSZOPD
COMPLETE
----
Once the job is complete you can try again to submit the prompt and this time you'll have the answer
[source,sh]
----
curl http://0.0.0.0:8080/bedrock/prompt/
The average price of natural gas between 2010 and 2011 was $3.93 per unit.
----
=== Running a full session with the Knowledge Base
It's interesting to use the ability to create a session with the Knowledge Base.
To do this there is a route you can run, composed of Kamelets, named `aws-bedrock-knowledgebase-session.camel.yaml`
This route will be triggered by a timer and then it will loop, asking random range average value for natural gas prices, re-using the same Session id.
You can run
[source,sh]
----
jbang -Dcamel.jbang.version=4.6.0-SNAPSHOT camel@apache/camel run --properties=application.properties aws-bedrock-knowledgebase-session.camel.yaml
----
This will give the following output
[source,sh]
----
2024-04-04 12:53:31.341 INFO 47329 --- [ main] el.impl.engine.AbstractCamelContext : Routes startup (total:1 started:1 kamelets:3)
2024-04-04 12:53:31.341 INFO 47329 --- [ main] el.impl.engine.AbstractCamelContext : Started route1 (kamelet://timer-source)
2024-04-04 12:53:31.341 INFO 47329 --- [ main] el.impl.engine.AbstractCamelContext : Apache Camel 4.6.0-SNAPSHOT (aws-bedrock-knowledgebase-session) started in 501ms (build:0ms init:0ms start:501ms)
2024-04-04 12:53:32.350 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:27 : New Request to Knowledge Base What is the average natural gas price between 1998 and 1999?
2024-04-04 12:53:35.620 INFO 47329 --- [ - timer://tick] log-sink : Exchange[ExchangePattern: InOnly, Headers: {CamelAwsBedrockAgentRuntimeCitations=[Citation(GeneratedResponsePart=GeneratedResponsePart(TextResponsePart=*** Sensitive Data Redacted ***), RetrievedReferences=[RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***)])], CamelAwsBedrockAgentRuntimeSessionId=dd187271-cff8-4730-a6a7-42136120eac7, Content-Type=text/plain}, BodyType: String, Body: The average natural gas price between 1998 and 1999 was $1.96.]
2024-04-04 12:53:35.621 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:39 : SessionId is dd187271-cff8-4730-a6a7-42136120eac7
2024-04-04 12:53:35.621 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:27 : New Request to Knowledge Base What is the average natural gas price between 2005 and 2013?
2024-04-04 12:53:39.428 INFO 47329 --- [ - timer://tick] log-sink : Exchange[ExchangePattern: InOnly, Headers: {CamelAwsBedrockAgentRuntimeCitations=[Citation(GeneratedResponsePart=GeneratedResponsePart(TextResponsePart=*** Sensitive Data Redacted ***), RetrievedReferences=[RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***), RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***), RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***)])], CamelAwsBedrockAgentRuntimeSessionId=dd187271-cff8-4730-a6a7-42136120eac7, Content-Type=text/plain}, BodyType: String, Body: The average natural gas price between 2005 and 2013 was approximately $6 per thousand cubic feet based on the monthly price data provided in search results 1, 2 and 3.]
2024-04-04 12:53:39.431 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:39 : SessionId is dd187271-cff8-4730-a6a7-42136120eac7
2024-04-04 12:53:39.433 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:27 : New Request to Knowledge Base What is the average natural gas price between 2017 and 2017?
2024-04-04 12:53:43.244 INFO 47329 --- [ - timer://tick] log-sink : Exchange[ExchangePattern: InOnly, Headers: {CamelAwsBedrockAgentRuntimeCitations=[Citation(GeneratedResponsePart=GeneratedResponsePart(TextResponsePart=*** Sensitive Data Redacted ***), RetrievedReferences=[RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***)])], CamelAwsBedrockAgentRuntimeSessionId=dd187271-cff8-4730-a6a7-42136120eac7, Content-Type=text/plain}, BodyType: String, Body: Based on the price data provided in search result 1, which contains daily natural gas prices between 2017-12-11 and 2017-12-29, the average natural gas price between 2017 and 2017 was approximately 2.8 dollars.]
2024-04-04 12:53:43.245 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:39 : SessionId is dd187271-cff8-4730-a6a7-42136120eac7
2024-04-04 12:53:43.245 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:27 : New Request to Knowledge Base What is the average natural gas price between 2015 and 2011?
2024-04-04 12:53:47.803 INFO 47329 --- [ - timer://tick] log-sink : Exchange[ExchangePattern: InOnly, Headers: {CamelAwsBedrockAgentRuntimeCitations=[Citation(GeneratedResponsePart=GeneratedResponsePart(TextResponsePart=*** Sensitive Data Redacted ***), RetrievedReferences=[RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***), RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***), RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***)])], CamelAwsBedrockAgentRuntimeSessionId=dd187271-cff8-4730-a6a7-42136120eac7, Content-Type=text/plain}, BodyType: String, Body: The average natural gas price between 2011 and 2015 was approximately $3.50 per thousand cubic feet based on the price data provided in search results 1, 2 and 3.]
2024-04-04 12:53:47.804 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:39 : SessionId is dd187271-cff8-4730-a6a7-42136120eac7
2024-04-04 12:53:47.805 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:27 : New Request to Knowledge Base What is the average natural gas price between 2018 and 2017?
2024-04-04 12:53:51.479 INFO 47329 --- [ - timer://tick] log-sink : Exchange[ExchangePattern: InOnly, Headers: {CamelAwsBedrockAgentRuntimeCitations=[Citation(GeneratedResponsePart=GeneratedResponsePart(TextResponsePart=*** Sensitive Data Redacted ***), RetrievedReferences=[RetrievedReference(Content=*** Sensitive Data Redacted ***, Location=*** Sensitive Data Redacted ***, Metadata=*** Sensitive Data Redacted ***)])], CamelAwsBedrockAgentRuntimeSessionId=dd187271-cff8-4730-a6a7-42136120eac7, Content-Type=text/plain}, BodyType: String, Body: The average natural gas price between 2018 and 2017 was $2.93 per thousand cubic feet.]
2024-04-04 12:53:51.481 INFO 47329 --- [ - timer://tick] knowledgebase-session.camel.yaml:39 : SessionId is dd187271-cff8-4730-a6a7-42136120eac7
^C2024-04-04 12:54:01.296 INFO 47329 --- [ main] el.impl.engine.AbstractCamelContext : Apache Camel 4.6.0-SNAPSHOT (aws-bedrock-knowledgebase-session) is shutting down (timeout:10s)
2024-04-04 12:54:01.307 INFO 47329 --- [ main] el.impl.engine.AbstractCamelContext : Routes stopped (total:1 stopped:1 kamelets:3)
2024-04-04 12:54:01.307 INFO 47329 --- [ main] el.impl.engine.AbstractCamelContext : Stopped route1 (kamelet://timer-source)
2024-04-04 12:54:01.313 INFO 47329 --- [ main] el.impl.engine.AbstractCamelContext : Apache Camel 4.6.0-SNAPSHOT (aws-bedrock-knowledgebase-session) shutdown in 17ms (uptime:30s)
2024-04-04 12:54:01.314 INFO 47329 --- [ main] org.apache.camel.main.MainSupport : Apache Camel (JBang) 4.6.0-SNAPSHOT shutdown
----
=== Cleanup AWS S3 bucket, SQS Queue and EventBridge Rule through Terraform
You'll need to cleanup everything from AWS console or CLI.
If you used terraform it will be enough to run terraform destroy
[source,sh]
----
cd terraform/
----
At this point you should be to run the destroy
[source,sh]
----
terraform destroy -var="s3_bucket_name=s3-bedrock-test" -var="sqs_queue_name=sqs-bedrock-test"
----
You'll need to specify the same var used for terraform apply.
In your AWS Bedrock console, delete the knowledge base and in Opensearch delete the vectorsearch collection.
=== Help and contributions
If you hit any problem using Camel or have some feedback, then please
https://camel.apache.org/community/support/[let us know].
We also love contributors, so
https://camel.apache.org/community/contributing/[get involved] :-)
The Camel riders!