From 1f9cab257dda89cf5a9420205b53f4a3638993e2 Mon Sep 17 00:00:00 2001 From: Guangya Liu Date: Fri, 16 Aug 2024 15:33:20 -0400 Subject: [PATCH] bedrock langchain --- aws/bedrock1.py | 2 -- aws/langchain-bedrock.py | 27 +++++++++++++++++++++++++++ haystack/hay-chat-bot.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 aws/langchain-bedrock.py create mode 100644 haystack/hay-chat-bot.py diff --git a/aws/bedrock1.py b/aws/bedrock1.py index 87c8d2e..6ce5ccc 100644 --- a/aws/bedrock1.py +++ b/aws/bedrock1.py @@ -1,5 +1,3 @@ - - from dotenv import load_dotenv load_dotenv() diff --git a/aws/langchain-bedrock.py b/aws/langchain-bedrock.py new file mode 100644 index 0000000..120d516 --- /dev/null +++ b/aws/langchain-bedrock.py @@ -0,0 +1,27 @@ +''' +(titan) gyliu@Guangyas-MacBook-Air .aws % cat credentials +[bedrock-admin] +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= + +(titan) gyliu@Guangyas-MacBook-Air .aws % pwd +/Users/gyliu/.aws +''' + +from dotenv import load_dotenv +load_dotenv() + +from traceloop.sdk import Traceloop +from traceloop.sdk.decorators import task, workflow + +Traceloop.init(app_name="joke_generation_service") + +from langchain_aws import BedrockLLM + +llm = BedrockLLM( + credentials_profile_name="bedrock-admin", + model_id="amazon.titan-text-express-v1", + region_name="us-west-2", +) + +print(llm.invoke(input="What is the recipe of mayonnaise?")) diff --git a/haystack/hay-chat-bot.py b/haystack/hay-chat-bot.py new file mode 100644 index 0000000..c24a280 --- /dev/null +++ b/haystack/hay-chat-bot.py @@ -0,0 +1,32 @@ +from dotenv import load_dotenv +load_dotenv() + +from haystack.telemetry import tutorial_running + +tutorial_running(40) +''' +from haystack.dataclasses import ChatMessage +from haystack.components.generators.chat import OpenAIChatGenerator + +messages = [ + ChatMessage.from_system("Always respond in German even if some input data is in other languages."), + ChatMessage.from_user("What's Natural Language Processing? Be brief."), +] + +chat_generator = OpenAIChatGenerator(model="gpt-3.5-turbo") +chat_generator.run(messages=messages) +''' +''' +from haystack.dataclasses import ChatMessage +from haystack.components.generators.chat import OpenAIChatGenerator +from haystack.components.generators.utils import print_streaming_chunk + +messages = [ + ChatMessage.from_system("Always respond in German even if some input data is in other languages."), + ChatMessage.from_user("What's Natural Language Processing? Be brief."), +] + +chat_generator = OpenAIChatGenerator(model="gpt-3.5-turbo", streaming_callback=print_streaming_chunk) +response = chat_generator.run(messages=messages) +print(response) +''' \ No newline at end of file