Skip to content

Commit 8e26f7b

Browse files
Bedrock integration (#85)
* Llama-index-upgrade * Upgrade SDK version * pdm lock file update * Token counter fixes * Bedrock integration * Update mistalai version to resolve backward comaptibility issues of latest release my mistralai --------- Signed-off-by: Gayathri <[email protected]>
1 parent 6c1178d commit 8e26f7b

File tree

7 files changed

+587
-318
lines changed

7 files changed

+587
-318
lines changed

pdm.lock

Lines changed: 396 additions & 318 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@ dependencies = [
3636
"llama-index-llms-openai==0.1.27",
3737
"llama-index-llms-palm==0.1.5",
3838
"llama-index-llms-mistralai==0.1.19",
39+
#Todo: Remove the version locking for mistralai below
40+
#once the the following error is resolved from mistralai side
41+
#Unable to import llm adapters : No module named 'mistralai.models.chat_completion'
42+
#Looks like mistralai>0.4.2 is not backward compatible
43+
"mistralai==0.4.2",
44+
3945
"llama-index-llms-anyscale==0.1.4",
4046
"llama-index-llms-anthropic==0.1.16",
4147
"llama-index-llms-azure-openai==0.1.10",
4248
"llama-index-llms-vertex==0.2.2",
4349
"llama-index-llms-replicate==0.1.3",
4450
"llama-index-llms-ollama==0.2.2",
51+
"llama-index-llms-bedrock==0.1.12",
4552
# For Llama Parse X2Text
4653
"llama-parse==0.4.9",
4754
# OCR
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Unstract Bedrock LLM Adapter
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[build-system]
2+
requires = ["pdm-backend"]
3+
build-backend = "pdm.backend"
4+
5+
6+
[project]
7+
name = "unstract-bedrock-llm"
8+
version = "0.0.1"
9+
description = "Bedrock LLM"
10+
authors = [
11+
{name = "Zipstack Inc.", email = "[email protected]"},
12+
]
13+
dependencies = [
14+
]
15+
requires-python = ">=3.9"
16+
readme = "README.md"
17+
classifiers = [
18+
"Programming Language :: Python"
19+
]
20+
license = {text = "MIT"}
21+
22+
[tool.pdm.build]
23+
includes = ["src"]
24+
package-dir = "src"
25+
# source-includes = ["tests"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .bedrock import BedrockLLM
2+
3+
metadata = {
4+
"name": BedrockLLM.__name__,
5+
"version": "1.0.0",
6+
"adapter": BedrockLLM,
7+
"description": "Bedrock LLM adapter",
8+
"is_active": True,
9+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import os
2+
from typing import Any, Optional
3+
4+
from llama_index.core.llms import LLM
5+
from llama_index.llms.bedrock import Bedrock
6+
7+
from unstract.sdk.adapters.exceptions import AdapterError
8+
from unstract.sdk.adapters.llm.constants import LLMKeys
9+
from unstract.sdk.adapters.llm.helper import LLMHelper
10+
from unstract.sdk.adapters.llm.llm_adapter import LLMAdapter
11+
12+
13+
class Constants:
14+
MODEL = "model"
15+
API_KEY = "api_key"
16+
TIMEOUT = "timeout"
17+
MAX_RETIRES = "max_retries"
18+
SECRET_ACCESS_KEY = "aws_secret_access_key"
19+
ACCESS_KEY_ID = "aws_access_key_id"
20+
REGION_NAME = "region_name"
21+
CONTEXT_SIZE = "context_size"
22+
23+
24+
class BedrockLLM(LLMAdapter):
25+
def __init__(self, settings: dict[str, Any]):
26+
super().__init__("Bedrock")
27+
self.config = settings
28+
29+
@staticmethod
30+
def get_id() -> str:
31+
return "bedrock|8d18571f-5e96-4505-bd28-ad0379c64064"
32+
33+
@staticmethod
34+
def get_name() -> str:
35+
return "Bedrock"
36+
37+
@staticmethod
38+
def get_description() -> str:
39+
return "Bedrock LLM"
40+
41+
@staticmethod
42+
def get_provider() -> str:
43+
return "bedrock"
44+
45+
@staticmethod
46+
def get_icon() -> str:
47+
return "/icons/adapter-icons/Bedrock.png"
48+
49+
@staticmethod
50+
def get_json_schema() -> str:
51+
f = open(f"{os.path.dirname(__file__)}/static/json_schema.json")
52+
schema = f.read()
53+
f.close()
54+
return schema
55+
56+
def get_llm_instance(self) -> LLM:
57+
try:
58+
context_size: Optional[int] = (
59+
int(self.config.get(Constants.CONTEXT_SIZE, 0))
60+
if self.config.get(Constants.CONTEXT_SIZE)
61+
else None
62+
)
63+
llm: LLM = Bedrock(
64+
model=self.config.get(Constants.MODEL),
65+
aws_access_key_id=self.config.get(Constants.ACCESS_KEY_ID),
66+
aws_secret_access_key=self.config.get(Constants.SECRET_ACCESS_KEY),
67+
region_name=self.config.get(Constants.REGION_NAME),
68+
timeout=float(
69+
self.config.get(Constants.TIMEOUT, LLMKeys.DEFAULT_TIMEOUT)
70+
),
71+
max_retries=int(
72+
self.config.get(Constants.MAX_RETIRES, LLMKeys.DEFAULT_MAX_RETRIES)
73+
),
74+
temperature=0,
75+
context_size=context_size,
76+
)
77+
return llm
78+
except Exception as e:
79+
raise AdapterError(str(e))
80+
81+
def test_connection(self) -> bool:
82+
llm = self.get_llm_instance()
83+
test_result: bool = LLMHelper.test_llm_instance(llm=llm)
84+
return test_result
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"title": "Bedrock LLM",
3+
"type": "object",
4+
"required": [
5+
"aws_secret_access_key",
6+
"region_name",
7+
"aws_access_key_id",
8+
"model",
9+
"adapter_name"
10+
],
11+
"properties": {
12+
"adapter_name": {
13+
"type": "string",
14+
"title": "Name",
15+
"default": "",
16+
"description": "Provide a unique name for this adapter instance. Example: Bedrock-1"
17+
},
18+
"model": {
19+
"type": "string",
20+
"title": "Model",
21+
"default": "amazon.titan-text-express-v1",
22+
"description": "Model name. Refer to Bedrock's documentation for the list of available models."
23+
},
24+
"context_size": {
25+
"type": "number",
26+
"minimum": 0,
27+
"multipleOf": 1,
28+
"title": "Context Size",
29+
"description": "The maximum number of context tokens for the model. For setting default in supportied models, leave this empty."
30+
},
31+
"aws_access_key_id": {
32+
"type": "string",
33+
"title": "AWS Access Key ID",
34+
"description": "Provide your AWS Access Key ID",
35+
"format": "password"
36+
},
37+
"aws_secret_access_key": {
38+
"type": "string",
39+
"title": "AWS Secret Access Key",
40+
"description": "Provide your AWS Secret Access Key",
41+
"format": "password"
42+
},
43+
"region_name": {
44+
"type": "string",
45+
"title": "AWS Region name",
46+
"description": "Provide the AWS Region name where the service is running. Eg. us-east-1"
47+
},
48+
"timeout": {
49+
"type": "number",
50+
"minimum": 0,
51+
"multipleOf": 1,
52+
"title": "Timeout",
53+
"default": 900,
54+
"description": "Timeout in seconds"
55+
},
56+
"max_retries": {
57+
"type": "number",
58+
"minimum": 0,
59+
"multipleOf": 1,
60+
"title": "Max Retries",
61+
"default": 5,
62+
"description": "Maximum number of retries to attempt when a request fails."
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)