Evan Lesmez commited on
Commit
4edc753
1 Parent(s): 7ca632f

Save dependencies & engineered recipe conversation

Browse files

poetry for dependencies.
env.example for promptlayer and openai API keys

.env.example ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ OPENAI_API_KEY = "sk-*"
2
+ PROMPTLAYER_API_KEY = "pl_*"
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
.vscode/settings.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "[python]": {
3
+ "editor.defaultFormatter": "ms-python.black-formatter"
4
+ },
5
+ "python.formatting.provider": "none"
6
+ }
chatbot/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from dotenv import load_dotenv
2
+
3
+ load_dotenv()
chatbot/engineer_prompt.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.chat_models import PromptLayerChatOpenAI
2
+ from langchain.schema import HumanMessage, AIMessage, SystemMessage
3
+ from langchain.chains import ConversationChain
4
+ from langchain.memory import ConversationBufferMemory
5
+ from langchain.prompts.chat import (
6
+ ChatPromptTemplate,
7
+ SystemMessagePromptTemplate,
8
+ HumanMessagePromptTemplate,
9
+ AIMessagePromptTemplate,
10
+ MessagesPlaceholder,
11
+ )
12
+
13
+ # TODO Multiple chains sequenced?
14
+
15
+ ingredients_prompt = ChatPromptTemplate.from_messages(
16
+ [
17
+ SystemMessagePromptTemplate.from_template(
18
+ """
19
+ The following is a conversation between a human and a friendly AI chef.
20
+ The AI is compassionate to animals and only recommends vegan recipes based on the ingredients, allergies, and other preferences the human has.
21
+
22
+ Knowledge: A vegan diet implies a plant-based diet avoiding all animal foods such as meat (including fish, shellfish and insects), dairy, eggs and honey
23
+
24
+ Let's think step by step.
25
+ If the human messages are unrelated to vegan recipes, remind them of your purpose to recommend vegan recipes.
26
+ """.strip()
27
+ ),
28
+ AIMessagePromptTemplate.from_template(
29
+ "What ingredients do you wish to cook with?"
30
+ ),
31
+ HumanMessagePromptTemplate.from_template("Ingredients: {ingredients}"),
32
+ AIMessagePromptTemplate.from_template(
33
+ "Do you have any allergies I should be aware of?"
34
+ ),
35
+ HumanMessagePromptTemplate.from_template("Allergies: {allergies}"),
36
+ AIMessagePromptTemplate.from_template(
37
+ "Do you have any preferences I should consider for the recipe such preparation time, difficulty, or cuisine region?"
38
+ ),
39
+ HumanMessagePromptTemplate.from_template(
40
+ """
41
+ Give me a vegan recipe that includes at least a few of the ingredients provided (if any).
42
+ Respect the human's allergies (if any).
43
+ Follow these other preferences as closely as possible if they are inline with your purpose of recommending vegan recipes:
44
+
45
+ ###
46
+ {recipe_freeform_input}
47
+ ###
48
+
49
+ Output format:
50
+
51
+ **Vegan recipe name**
52
+ Preparation time (humanized)
53
+
54
+ Ingredients (List of ingredients with quantities):
55
+ - <quantity and unit> <ingredient>
56
+
57
+ Steps (detailed):
58
+ 1.
59
+ 2.
60
+ 3.
61
+ ...
62
+ """.strip()
63
+ ),
64
+ ]
65
+ )
66
+
67
+ # MessagesPlaceholder(variable_name="history"),
68
+ # HumanMessagePromptTemplate.from_template("{input}"),
69
+ chat = PromptLayerChatOpenAI(temperature=1, pl_tags=["langchain"], return_pl_id=True)
70
+ memory = ConversationBufferMemory(return_messages=True)
71
+
72
+ chat_msgs = ingredients_prompt.format_prompt(
73
+ ingredients="tofu, pickles, olives, tomatoes, lettuce, bell peppers, carrots, bread",
74
+ allergies="",
75
+ recipe_freeform_input="The preparation time should be less than 30 minutes. I really love Thai food!",
76
+ )
77
+ chat_msgs = chat_msgs.to_messages()
78
+ results = chat.generate([chat_msgs])
79
+ chat_msgs.extend(
80
+ [
81
+ results.generations[0][0].message,
82
+ MessagesPlaceholder(variable_name="history"),
83
+ HumanMessagePromptTemplate.from_template("{input}"),
84
+ ]
85
+ )
86
+ open_prompt = ChatPromptTemplate.from_messages(chat_msgs)
87
+ conversation = ConversationChain(
88
+ llm=chat, verbose=True, memory=memory, prompt=open_prompt
89
+ )
90
+
91
+ result = conversation.predict(input="Recommend a different recipe please.")
92
+ print(result)
93
+
94
+ #! PL score example
95
+ # chat_results = chat.generate([[HumanMessage(content=prompt)]])
96
+
97
+ # for res in chat_results.generations:
98
+ # pl_request_id = res[0].generation_info["pl_request_id"]
99
+ # print(res[0].text)
100
+ # score = int(input("Enter a score from 0 to 100 for how the prompt performed: "))
101
+ # promptlayer.track.score(request_id=pl_request_id, score=score)
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "lv-recipe-chatbot"
3
+ version = "0.1.0"
4
+ description = "Chatbot for recommending vegan recipes"
5
+ authors = ["Evan Lesmez <evanl@animalequality.org>"]
6
+ readme = "README.md"
7
+ packages = [{include = "lv_recipe_chatbot"}]
8
+
9
+ [tool.poetry.dependencies]
10
+ python = "^3.10"
11
+ langchain = "^0.0.145"
12
+ openai = "^0.27.4"
13
+ gradio = "^3.27.0"
14
+ jupyterlab = "^3.6.3"
15
+ tqdm = "^4.65.0"
16
+ transformers = "^4.28.1"
17
+ torch = "^2.0.0"
18
+ promptlayer = "^0.1.80"
19
+ python-dotenv = "^1.0.0"
20
+
21
+ [tool.poetry.group.dev.dependencies]
22
+ black = "^23.3.0"
23
+ pytest = "^7.3.1"
24
+ mypy = "^1.2.0"
25
+
26
+
27
+ [build-system]
28
+ requires = ["poetry-core"]
29
+ build-backend = "poetry.core.masonry.api"