-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ysekiy/dev/bedrock
Bedrock support
- Loading branch information
Showing
10 changed files
with
114 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
amplify/backend/api/fargate/src/langchain/.env.development-template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
AWS_REGION=us-west-2 | ||
AWS_BEDROCK_REGION=us-east-1 | ||
ALLOW_ORIGINS=* | ||
SAGEMAKER_ENDPOINT_NAME=Rinna-Inference | ||
KENDRA_INDEX_ID=******* | ||
ANTHROPIC_API_KEY= | ||
LLM=rinna | ||
LLM=claude_bedrock | ||
AWS_ACCESS_KEY_ID=******* | ||
AWS_SECRET_ACCESS_KEY=******* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
amplify/backend/api/fargate/src/langchain/app/chain/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from chain.claude import build_claude_chain, build_claude_chain_without_doc, run_claude_chain | ||
from chain.claude_bedrock import build_claude_bedrock_chain, build_claude_bedrock_chain_without_doc, run_claude_bedrock_chain | ||
from chain.rinna import build_rinna_chain, run_rinna_chain |
83 changes: 83 additions & 0 deletions
83
amplify/backend/api/fargate/src/langchain/app/chain/claude_bedrock.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# Licensed under the MIT-0 License (https://github.com/aws/mit-0) | ||
"""claude on Bedrock を使った chain を定義するモジュール | ||
""" | ||
import os | ||
from typing import List | ||
|
||
from langchain.chains import LLMChain | ||
from langchain.llms.bedrock import Bedrock | ||
from langchain.prompts import PromptTemplate | ||
from schemas import KendraDocument, LLMWithDocReqBody | ||
|
||
model_id = "anthropic.claude-v1" | ||
bedrock_region = os.environ.get("AWS_BEDROCK_REGION", "us-east-1") | ||
|
||
|
||
def build_claude_bedrock_chain(): | ||
"""claude を LLM として利用する場合の Chain の作成""" | ||
|
||
inference_modifier = { | ||
"max_tokens_to_sample": 500, | ||
"temperature": 0.85, | ||
"top_k": 10, | ||
"top_p": 0.85, | ||
"stop_sequences": ["\n\nHuman:"] | ||
} | ||
|
||
claude = Bedrock( | ||
model_id=model_id, | ||
region_name=bedrock_region, | ||
model_kwargs=inference_modifier | ||
) | ||
prompt = PromptTemplate( | ||
template="""Human: 資料: | ||
{context} | ||
上記の資料をもとに以下の質問に回答しなさい。[0]の形式で参考にした資料を示しなさい。また資料がないものは「わかりません」と答えなさい。\n質問: | ||
{question} | ||
Assistant:""", | ||
input_variables=["context", "question"], | ||
) | ||
return LLMChain(llm=claude, prompt=prompt) | ||
|
||
|
||
def build_claude_bedrock_chain_without_doc(): | ||
"""context が与えられていない場合のプロンプトを使う Chain の作成""" | ||
|
||
inference_modifier = { | ||
"max_tokens_to_sample": 200, | ||
"temperature": 0.85, | ||
"top_k": 10, | ||
"top_p": 0.85, | ||
"stop_sequences": ["\n\nHuman:"] | ||
} | ||
|
||
claude = Bedrock( | ||
model_id=model_id, | ||
region_name=bedrock_region, | ||
model_kwargs=inference_modifier | ||
) | ||
prompt = PromptTemplate( | ||
template="""Human: {question} | ||
Assistant:""", | ||
input_variables=["question"], | ||
) | ||
return LLMChain(llm=claude, prompt=prompt) | ||
|
||
|
||
def run_claude_bedrock_chain(chain: LLMChain, body: LLMWithDocReqBody): | ||
"""claude の Chain を実行する""" | ||
return chain.run( | ||
context=_make_context_for_claude_from_docs(body.documents), | ||
question=body.userUtterance, | ||
) | ||
|
||
|
||
def _make_context_for_claude_from_docs(documents: List[KendraDocument]): | ||
"""与えられた Document 情報から claude のプロンプトに埋め込むための context 情報を作成する""" | ||
context: str = "" | ||
for doc_id, doc in enumerate(documents): | ||
context += f"[{doc_id}]{doc.title}\n{doc.excerpt}\n" | ||
return context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
langchain==0.0.223 | ||
boto3>=1.26.137 | ||
langchain==0.0.310 | ||
boto3>=1.28.62 | ||
fastapi>=0.95.2 | ||
uvicorn>=0.22.0 | ||
anthropic==0.2.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters