0
0
Prompt Engineering / GenAIml~10 mins

LangChain agents in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic LangChain agent using an OpenAI LLM.

Prompt Engineering / GenAI
from langchain.agents import initialize_agent
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
agent = initialize_agent(llm, tools=[], agent= [1] )
Drag options to blanks, or click blank then click option'
A"zero-shot-react-description"
B"chat-completion"
C"simple-agent"
D"tool-using-agent"
Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid agent type string.
Confusing agent types that require tools with those that don't.
2fill in blank
medium

Complete the code to add a tool to the LangChain agent's tool list.

Prompt Engineering / GenAI
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from langchain.tools import Tool

def dummy_tool_func(input_text):
    return "Processed: " + input_text

tool = Tool(name="DummyTool", func=[1], description="A dummy tool.")

llm = OpenAI(temperature=0)
agent = initialize_agent(llm, tools=[tool], agent="zero-shot-react-description")
Drag options to blanks, or click blank then click option'
Alambda x: x
Bdummy_tool_func()
C"dummy_tool_func"
Ddummy_tool_func
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function call instead of the function object.
Passing the function name as a string.
3fill in blank
hard

Fix the error in the code to run the LangChain agent with an input query.

Prompt Engineering / GenAI
response = agent.[1]("What is the capital of France?")
Drag options to blanks, or click blank then click option'
Arespond
Brun
Ccall
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent method names like 'execute' or 'call'.
Confusing agent methods with other API methods.
4fill in blank
hard

Fill both blanks to create a tool that returns the length of the input text and add it to the agent.

Prompt Engineering / GenAI
from langchain.tools import Tool

def length_tool_func(text):
    return len(text)

tool = Tool(name=[1], func=[2], description="Returns length of input text.")
Drag options to blanks, or click blank then click option'
A"LengthTool"
Blength_tool_func
C"TextLength"
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the built-in 'len' function instead of the custom function.
Using a non-string for the tool name.
5fill in blank
hard

Fill all three blanks to create a LangChain agent with a tool that reverses input text and run it on a query.

Prompt Engineering / GenAI
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from langchain.tools import Tool

def reverse_text(text):
    return text[::-1]

reverse_tool = Tool(name=[1], func=[2], description="Reverses input text.")

llm = OpenAI(temperature=0)
agent = initialize_agent(llm, tools=[reverse_tool], agent=[3])

result = agent.run("Hello World")
Drag options to blanks, or click blank then click option'
A"ReverseTool"
Breverse_text
C"zero-shot-react-description"
D"reverse-agent"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect agent type strings.
Passing function names as strings instead of function objects.