|
1 | 1 | from langchain.chat_models import ChatOpenAI |
2 | 2 | from langchain.prompts.chat import ( |
3 | | - PromptTemplate |
| 3 | + ChatPromptTemplate, |
| 4 | + SystemMessagePromptTemplate, |
| 5 | + HumanMessagePromptTemplate, |
4 | 6 | ) |
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 |
7 | 9 |
|
8 | 10 |
|
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) |
13 | 19 |
|
| 20 | + human_template_travel_agent = "{text}" |
| 21 | + human_message_prompt = HumanMessagePromptTemplate.from_template( |
| 22 | + human_template_travel_agent) |
14 | 23 |
|
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]) |
17 | 27 |
|
| 28 | + chain = LLMChain( |
| 29 | + llm=ChatOpenAI(temperature=1), |
| 30 | + prompt=chat_prompt |
| 31 | + ) |
18 | 32 |
|
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)) |
22 | 36 |
|
23 | | -parser = PydanticOutputParser(pydantic_object=PeopleList) |
| 37 | + return recommendations |
24 | 38 |
|
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 | | -) |
31 | 39 |
|
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 |
33 | 64 |
|
34 | | -model = ChatOpenAI() |
35 | | -output = model.predict(_input.to_string()) |
36 | 65 |
|
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) |
0 commit comments