Skip to content

Commit 364ccad

Browse files
authored
Add support for string message in Bedrock textgen (#1291)
* Add support for string message in bedrock, update README * Add test for string message in test script Signed-off-by: Jonathan Minkin <[email protected]>
1 parent 625aec9 commit 364ccad

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

comps/llms/src/text-generation/README_bedrock.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
In order to start Bedrock service, you need to setup the following environment variables first.
1010

1111
```bash
12+
export AWS_REGION=${aws_region}
1213
export AWS_ACCESS_KEY_ID=${aws_access_key_id}
1314
export AWS_SECRET_ACCESS_KEY=${aws_secret_access_key}
1415
```
@@ -23,13 +24,13 @@ export AWS_SESSION_TOKEN=${aws_session_token}
2324

2425
```bash
2526
cd GenAIComps/
26-
docker build --no-cache -t opea/bedrock:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/llms/src/text-generation/Dockerfile .
27+
docker build --no-cache -t opea/llm-textgen:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/llms/src/text-generation/Dockerfile .
2728
```
2829

2930
## Run the Bedrock Microservice
3031

3132
```bash
32-
docker run -d --name bedrock -p 9009:9000 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e LLM_COMPONENT_NAME="OpeaTextGenBedrock" -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN opea/bedrock:latest
33+
docker run -d --name bedrock -p 9009:9000 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e LLM_COMPONENT_NAME="OpeaTextGenBedrock" -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN -e BEDROCK_REGION=$AWS_REGION opea/llm-textgen:latest
3334
```
3435

3536
(You can remove `-e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN` if you are not using an IAM Role)
@@ -42,6 +43,7 @@ curl http://${host_ip}:9009/v1/chat/completions \
4243
-d '{"model": "us.anthropic.claude-3-5-haiku-20241022-v1:0", "messages": [{"role": "user", "content": "What is Deep Learning?"}], "max_tokens":17}' \
4344
-H 'Content-Type: application/json'
4445

46+
# stream mode
4547
curl http://${host_ip}:9009/v1/chat/completions \
4648
-X POST \
4749
-d '{"model": "us.anthropic.claude-3-5-haiku-20241022-v1:0", "messages": [{"role": "user", "content": "What is Deep Learning?"}], "max_tokens":17, "stream": "true"}' \

comps/llms/src/text-generation/integrations/bedrock.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,21 @@ async def invoke(self, input: ChatCompletionRequest):
8787
if logflag and len(inference_config) > 0:
8888
logger.info(f"[llm - chat] inference_config: {inference_config}")
8989

90-
# Parse messages from HuggingFace TGI format to bedrock messages format
91-
# tgi: [{role: "system" | "user", content: "text"}]
90+
# Parse messages to Bedrock format
91+
# tgi: "prompt" or [{role: "system" | "user", content: "text"}]
9292
# bedrock: [role: "assistant" | "user", content: {text: "content"}]
93-
messages = [
94-
{"role": "assistant" if i.get("role") == "system" else "user", "content": [{"text": i.get("content", "")}]}
95-
for i in input.messages
96-
]
93+
messages = None
94+
if isinstance(input.messages, str):
95+
messages = [{"role": "user", "content": [{"text": input.messages}]}]
96+
else:
97+
# Convert from list of HuggingFace TGI message objects
98+
messages = [
99+
{
100+
"role": "assistant" if i.get("role") == "system" else "user",
101+
"content": [{"text": i.get("content", "")}],
102+
}
103+
for i in input.messages
104+
]
97105

98106
# Bedrock requires that conversations start with a user prompt
99107
# TGI allows the first message to be an assistant prompt, defining assistant behavior

tests/llms/test_llms_text-generation_bedrock.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,25 @@ function validate_microservice() {
6868
-H 'Content-Type: application/json')
6969

7070
if [[ $result == *"data: [DONE]"* ]]; then
71-
echo "Result correct."
71+
echo "Result correct using chat messages."
7272
echo "$result" >> ${LOG_PATH}/bedrock.log
7373
else
74-
echo "Result wrong. Received was $result"
74+
echo "Result wrong for chat messages. Received was $result"
75+
docker logs bedrock-test >> ${LOG_PATH}/bedrock.log
76+
exit 1
77+
fi
78+
79+
# Test string message
80+
result=$(http_proxy="" curl http://${ip_address}:${bedrock_port}/v1/chat/completions \
81+
-X POST \
82+
-d '{"model": "us.anthropic.claude-3-haiku-20240307-v1:0", "messages": "What is Deep Learning?", "max_tokens":17, "stream": "true"}' \
83+
-H 'Content-Type: application/json')
84+
85+
if [[ $result == *"data: [DONE]"* ]]; then
86+
echo "Result correct using string message."
87+
echo "$result" >> ${LOG_PATH}/bedrock.log
88+
else
89+
echo "Result wrong for string message. Received was $result"
7590
docker logs bedrock-test >> ${LOG_PATH}/bedrock.log
7691
exit 1
7792
fi

0 commit comments

Comments
 (0)