Skip to content

Commit ea303e8

Browse files
committed
Complete
1 parent 2ccf586 commit ea303e8

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

src/main.py

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,71 @@
11
from langchain.chat_models import ChatOpenAI
22
from langchain.prompts.chat import (
3-
PromptTemplate
3+
ChatPromptTemplate,
4+
SystemMessagePromptTemplate,
5+
HumanMessagePromptTemplate,
46
)
5-
from langchain.output_parsers import PydanticOutputParser
6-
from pydantic import BaseModel, Field
7+
from langchain.chains import LLMChain
8+
from langchain.schema import BaseOutputParser
79

810

9-
class Person(BaseModel):
10-
first_name: str = Field(description="first name")
11-
last_name: str = Field(description="last name")
12-
dob: str = Field(description="date of birth")
11+
def generate_travel_recommendations(travel_requests):
12+
"""
13+
Generate travel recommendations based on user requests
14+
"""
15+
# create templates
16+
system_template_travel_agent = """You are travel recommendation agent. Provide a short recommendation based on the user request."""
17+
system_message_prompt = SystemMessagePromptTemplate.from_template(
18+
system_template_travel_agent)
1319

20+
human_template_travel_agent = "{text}"
21+
human_message_prompt = HumanMessagePromptTemplate.from_template(
22+
human_template_travel_agent)
1423

15-
class PeopleList(BaseModel):
16-
people: list[Person] = Field(description="A list of people")
24+
# create full prompt
25+
chat_prompt = ChatPromptTemplate.from_messages(
26+
[system_message_prompt, human_message_prompt])
1727

28+
chain = LLMChain(
29+
llm=ChatOpenAI(temperature=1),
30+
prompt=chat_prompt
31+
)
1832

19-
model = ChatOpenAI(model="gpt-4")
20-
people_data = model.predict(
21-
"Generate a list of 10 fake peoples information. Only return the list. Each person should have a first name, last name and date of birth.")
33+
recommendations = []
34+
for travel_request in travel_requests:
35+
recommendations.append(chain.run(travel_request))
2236

23-
parser = PydanticOutputParser(pydantic_object=PeopleList)
37+
return recommendations
2438

25-
prompt = PromptTemplate(
26-
template="Answer the user query.\n{format_instructions}\n{query}\n",
27-
input_variables=["query"],
28-
partial_variables={
29-
"format_instructions": parser.get_format_instructions()},
30-
)
3139

32-
_input = prompt.format_prompt(query=people_data)
40+
def generate_travel_requests(n=5) -> list[str]:
41+
""" Generate travel requests
42+
n: number of requests
43+
"""
44+
# create templates
45+
system_template_travel_agent = """Generate one utterance for how someone would to travel for a {text}"""
46+
system_message_prompt = SystemMessagePromptTemplate.from_template(
47+
system_template_travel_agent)
48+
49+
# create full prompt
50+
chat_prompt = ChatPromptTemplate.from_messages(
51+
[system_message_prompt])
52+
53+
chain = LLMChain(
54+
llm=ChatOpenAI(model='gpt-4'),
55+
prompt=chat_prompt
56+
)
57+
58+
results = []
59+
60+
for _ in range(0, n):
61+
results.append(chain.run("beach vacation"))
62+
63+
return results
3364

34-
model = ChatOpenAI()
35-
output = model.predict(_input.to_string())
3665

37-
parsed = parser.parse(output)
38-
print(parsed)
66+
# generate some requests
67+
travel_requests = generate_travel_requests()
68+
print(travel_requests)
69+
# get the recommendations
70+
recommendations = generate_travel_recommendations(travel_requests)
71+
print(recommendations)

src/requirements.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
openai==0.27.8
22
python-dotenv==1.0.0
3-
langchain==0.0.242
4-
beautifulsoup4==4.12.2
5-
faiss-cpu==1.7.4
6-
tiktoken==0.4.0
3+
langchain==0.0.242

0 commit comments

Comments
 (0)