Skip to content

Instantly share code, notes, and snippets.

@nashid
Last active June 13, 2023 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nashid/55c2f866b3f8ba76bf670d9877457a37 to your computer and use it in GitHub Desktop.
Save nashid/55c2f866b3f8ba76bf670d9877457a37 to your computer and use it in GitHub Desktop.

Basics

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

πŸ“ƒ LLMs and Prompts

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))

πŸ“ƒ Meaning of roles in the API of GPT-4/ChatGPT (system/user/assistant)

ChatGPT API Transition Guide

  1. 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"
       }
      ]
     }
  1. 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"
       }
      ]
     }
  1. 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."
       }
      ]
     }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment