-
Notifications
You must be signed in to change notification settings - Fork 0
/
langchain_helper.py
67 lines (58 loc) · 2.41 KB
/
langchain_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# from langchain.llms import OpenAI
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain
from secret_key import openapi_key
import os
os.environ['OPENAI_API_KEY'] = openapi_key
llm = OpenAI(temperature=0.7)
# def generate_restaurant_name_and_items(cuisine):
# # Chain 1: Restaurant Name
# prompt_template_name = PromptTemplate(
# input_variables=['cuisine'],
# template="I want to open a restaurant for {cuisine} food. Suggest a fancy name for this."
# )
#
# name_chain = LLMChain(llm=llm, prompt=prompt_template_name, output_key="restaurant_name")
# # name_chain = prompt(llm=llm, prompt=prompt_template_name, output_key="restaurant_name")
#
# # Chain 2: Menu Items
# prompt_template_items = PromptTemplate(
# input_variables=['restaurant_name'],
# template="""Suggest some menu items for {restaurant_name}. Return it as a comma separated string"""
# )
#
# food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items, output_key="menu_items")
# # food_items_chain = prompt(llm=llm, prompt=prompt_template_items, output_key="menu_items")
#
# chain = SequentialChain(
# chains=[name_chain, food_items_chain],
# input_variables=['cuisine'],
# output_variables=['restaurant_name', "menu_items"]
# )
#
# response = chain({'cuisine': cuisine})
#
# return response
def generate_restaurant_name_and_items(cuisine):
# Define the prompt template for generating the restaurant name
prompt_template_name = PromptTemplate.from_template(
"I want to open a restaurant for {cuisine} food. Suggest a fancy name for this."
)
# Generate the restaurant name
name_prompt = prompt_template_name.format(cuisine=cuisine)
restaurant_name = llm.invoke(name_prompt)
# Define the prompt template for generating the menu items
prompt_template_items = PromptTemplate.from_template(
"Suggest some menu items for {restaurant_name}. Return it as a comma separated string."
)
# Generate the menu items
items_prompt = prompt_template_items.format(restaurant_name=restaurant_name)
menu_items = llm.invoke(items_prompt)
return {
"restaurant_name": restaurant_name,
"menu_items": menu_items
}
if __name__ == "__main__":
print(generate_restaurant_name_and_items("Nigerian"))