There are six main areas that LangChain is designed to help with. These are, in increasing order of complexity:
- π LLMs and Prompts
- π Chains
- π Data Augmented Generation
- π€ Agents
- π§ Memory
- π§ Evaluation
Asking Multiple Questions: If weβd like to ask multiple questions, we can try two approaches:
- Iterate through all questions using the generate method, answering them one at a time.
qs = [
{'question': "Which NFL team won the Super Bowl in the 2010 season?"},
{'question': "If I am 6 ft 4 inches, how tall am I in centimeters?"},
{'question': "Who was the 12th person on the moon?"},
{'question': "How many eyes does a blade of grass have?"}
]
res = llm_chain.generate(qs)
res
LLMResult(generations=[[Generation(text='green bay packers', generation_info=None)], [Generation(text='184', generation_info=None)], [Generation(text='john glenn', generation_info=None)], [Generation(text='one', generation_info=None)]], llm_output=None)
- Place all questions into a single prompt for the LLM; this will only work for more advanced LLMs.
multi_template = """Answer the following questions one at a time.
Questions:
{questions}
Answers:
"""
long_prompt = PromptTemplate(template=multi_template, input_variables=["questions"])
llm_chain = LLMChain(
prompt=long_prompt,
llm=flan_t5
)
qs_str = (
"Which NFL team won the Super Bowl in the 2010 season?\n" +
"If I am 6 ft 4 inches, how tall am I in centimeters?\n" +
"Who was the 12th person on the moon?" +
"How many eyes does a blade of grass have?"
)
print(llm_chain.run(qs_str))
- Role "user" : It means you or who is chatting or who is asking to chat gpt.
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "tell me a joke"
}
]
}
- Role "assistant" : It means open AI(chat gpt) server - who is replying your("user" role) questions.
{
"id": "chatcmpl-87n798n6bv4678",
"object": "chat.completion",
"created": 1683212418,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 12,
"completion_tokens": 18,
"total_tokens": 30
},
"choices": [
{
"message": {
"role": "assistant",
"content": "why did the chicken cross the road"
},
"finish_reason": "stop",
"index": 0
}
]
}
If need to continuous chat conversation with previous context- text from both role (user + assistant) need to send to the API.
Example: (API Request)
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "tell me a joke"
},
{
"role": "assistant",
"content": "why did the chicken cross the road"
},
{
"role": "user",
"content": "I don't know, why did the chicken cross the road"
}
]
}
- Role "system" : It means the system developer who can internally give some instructions for the conversation. developer can provide option for user input also which depends on the system requirements.
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "tell me a joke"
},
{
"role": "system",
"content": "You are an assistant that speaks like Shakespeare."
}
]
}