Skip to content

Commit aee5dae

Browse files
author
Haoping Liu
committed
1. Replace claude-2 with claude-3-5, as access to claude-2 has become more restricted.
2. Use the langchain_aws package instead of langchain.llms.bedrock, as the latter has been deprecated. 3. Transition to langchain_core.prompts.PromptTemplate in place of langchain.PromptTemplate, as the previous package is deprecated
1 parent 090f7ac commit aee5dae

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Diff for: ingredient-to-recipe/app.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
22
from dotenv import load_dotenv
3-
from langchain import PromptTemplate, LLMChain
3+
from langchain_core.prompts import PromptTemplate
44
from transformers import pipeline
55
import streamlit as st
6-
from langchain.llms.bedrock import Bedrock
6+
import boto3
7+
from langchain_aws import ChatBedrock
78

89
PAGE_CONFIG = {"page_title":"Image to Recipe", "page_icon":":chef:", "layout":"centered"}
910
st.set_page_config(**PAGE_CONFIG)
@@ -24,8 +25,14 @@
2425
""", unsafe_allow_html=True)
2526

2627
def get_llm():
27-
bedrock_llm = Bedrock(model_id="anthropic.claude-v2",
28-
model_kwargs={"temperature": 0.7, "max_tokens_to_sample": 4096})
28+
29+
bedrock_client = boto3.client('bedrock-runtime')
30+
bedrock_llm = ChatBedrock(
31+
provider="anthropic",
32+
model_id="anthropic.claude-3-5-sonnet-20240620-v1:0",
33+
client = bedrock_client,
34+
model_kwargs={"max_tokens": 4096, "temperature": 0.7},
35+
)
2936
return bedrock_llm
3037

3138
def image_to_text(url):
@@ -87,12 +94,12 @@ def generate_recipe(ingredients):
8794
"""
8895

8996
with st.spinner('Making the recipe for you...'):
90-
prompt = PromptTemplate(template=template, input_variables=["ingredients"])
97+
prompt = PromptTemplate.from_template(template=template)
98+
prompt_formatted_str: str = prompt.format(ingredients=ingredients)
9199
llm = get_llm()
92-
recipe_chain = LLMChain(llm=llm, prompt=prompt, verbose=True)
93-
recipe = recipe_chain.run(ingredients)
100+
recipe = llm.invoke(input=prompt_formatted_str)
94101

95-
return recipe
102+
return recipe.content
96103

97104
def main():
98105

@@ -151,8 +158,5 @@ def main():
151158
os.remove(upload_file.name)
152159

153160
if __name__ == "__main__":
154-
load_dotenv()
155-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
156-
HUGGINFACE_HUB_API_TOKEN = os.getenv("HUGGINFACE_HUB_API_TOKEN")
157-
161+
load_dotenv()
158162
main()

0 commit comments

Comments
 (0)