amazon personalize langchain extensions
1.0.0
이 저장소는 Langchain과 함께 작동하는 유틸리티 클래스 세트를 제공합니다. 현재 Amazon Personalize 캠페인/추천자 및 AmazonPersonalizeChain
사용자 지정 체인 빌드와 함께 작동하여 Amazon Personalize에서 권장 사항을 검색하고 기본 프롬프트(사용자가 재정의할 수 있음)를 실행하기 위한 유틸리티 클래스 AmazonPersonalize
가 있습니다.
저장소 복제
git clone https://github.com/aws-samples/amazon-personalize-langchain-extensions.git
repo 디렉토리로 이동
cd amazon-personalize-langchain-extensions
클래스 설치
pip install .
from aws_langchain import AmazonPersonalize
recommender_arn = "<insert_arn>"
client = AmazonPersonalize ( credentials_profile_name = "default" , region_name = "us-west-2" , recommender_arn = recommender_arn )
client . get_recommendations ( user_id = "1" )
from aws_langchain import AmazonPersonalize
from aws_langchain import AmazonPersonalizeChain
from langchain . llms . bedrock import Bedrock
recommender_arn = "<insert_arn>"
bedrock_llm = Bedrock ( model_id = "anthropic.claude-v2" , region_name = "us-west-2" )
client = AmazonPersonalize ( credentials_profile_name = "default" , region_name = "us-west-2" , recommender_arn = recommender_arn )
# Create personalize chain
# Use return_direct=True if you do not want summary
chain = AmazonPersonalizeChain . from_llm (
llm = bedrock_llm ,
client = client ,
return_direct = False
)
response = chain ({ 'user_id' : '1' })
print ( response )
from langchain . prompts . prompt import PromptTemplate
from aws_langchain import AmazonPersonalize
from aws_langchain import AmazonPersonalizeChain
from langchain . llms . bedrock import Bedrock
RANDOM_PROMPT_QUERY = """
You are a skilled publicist. Write a high-converting marketing email advertising several movies available in a video-on-demand streaming platform next week,
given the movie and user information below. Your email will leverage the power of storytelling and persuasive language.
The movies to recommend and their information is contained in the <movie> tag.
All movies in the <movie> tag must be recommended. Give a summary of the movies and why the human should watch them.
Put the email between <email> tags.
<movie>
{result}
</movie>
Assistant:
"""
RANDOM_PROMPT = PromptTemplate ( input_variables = [ "result" ], template = RANDOM_PROMPT_QUERY )
recommender_arn = "<insert_arn>"
bedrock_llm = Bedrock ( model_id = "anthropic.claude-v2" , region_name = "us-west-2" )
client = AmazonPersonalize ( credentials_profile_name = "default" , region_name = "us-west-2" , recommender_arn = recommender_arn )
chain = AmazonPersonalizeChain . from_llm ( llm = bedrock_llm , client = client , return_direct = False , prompt_template = RANDOM_PROMPT )
chain . run ({ 'user_id' : '1' , 'item_id' : '234' })
from langchain . chains import SequentialChain
from langchain . chains import LLMChain
from aws_langchain import AmazonPersonalize
from aws_langchain import AmazonPersonalizeChain
from langchain . llms . bedrock import Bedrock
from langchain . prompts . prompt import PromptTemplate
RANDOM_PROMPT_QUERY_2 = """
You are a skilled publicist. Write a high-converting marketing email advertising several movies available in a video-on-demand streaming platform next week,
given the movie and user information below. Your email will leverage the power of storytelling and persuasive language.
You want the email to impress the user, so make it appealing to them.
The movies to recommend and their information is contained in the <movie> tag.
All movies in the <movie> tag must be recommended. Give a summary of the movies and why the human should watch them.
Put the email between <email> tags.
<movie>
{result}
</movie>
Assistant:
"""
recommender_arn = "<insert_arn>"
bedrock_llm = Bedrock ( model_id = "anthropic.claude-v2" , region_name = "us-west-2" )
client = AmazonPersonalize ( credentials_profile_name = "default" , region_name = "us-west-2" , recommender_arn = recommender_arn )
RANDOM_PROMPT_2 = PromptTemplate ( input_variables = [ "result" ], template = RANDOM_PROMPT_QUERY_2 )
personalize_chain_instance = AmazonPersonalizeChain . from_llm ( llm = bedrock_llm , client = client , return_direct = True )
random_chain_instance = LLMChain ( llm = bedrock_llm , prompt = RANDOM_PROMPT_2 )
overall_chain = SequentialChain ( chains = [ personalize_chain_instance , random_chain_instance ], input_variables = [ "user_id" ], verbose = True )
overall_chain . run ({ 'user_id' : '1' , 'item_id' : '234' })
from aws_langchain import AmazonPersonalize
from aws_langchain import AmazonPersonalizeChain
from langchain . llms . bedrock import Bedrock
recommender_arn = "<insert_arn>"
metadata_column_list = [ "METADATA_COL1" ]
metadataMap = { "ITEMS" : metadata_column_list }
bedrock_llm = Bedrock ( model_id = "anthropic.claude-v2" , region_name = "us-west-2" )
client = AmazonPersonalize ( credentials_profile_name = "default" , region_name = "us-west-2" , recommender_arn = recommender_arn )
# Create personalize chain
# Use return_direct=True if you do not want summary
chain = AmazonPersonalizeChain . from_llm (
llm = bedrock_llm ,
client = client ,
return_direct = False
)
response = chain ({ 'user_id' : '1' , 'metadata_columns' : metadataMap })
print ( response )
pip uninstall aws-langchain
GitHub 브랜치를 생성하고 끌어오기 요청을 하세요. 자세한 내용은 기여를 참조하세요.
이 라이브러리는 MIT-0 라이선스에 따라 라이선스가 부여됩니다. 라이센스 파일을 참조하십시오.