0
0
LangChainframework~20 mins

OpenAI functions agent in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OpenAI Functions Agent Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this LangChain OpenAI functions agent call?
Given the following code snippet using LangChain's OpenAI functions agent, what will be the printed output?
LangChain
from langchain.agents import create_openai_functions_agent
from langchain.chat_models import ChatOpenAI

def greet(name: str) -> str:
    return f"Hello, {name}!"

llm = ChatOpenAI(temperature=0)
agent = create_openai_functions_agent(llm, [greet])

response = agent.invoke({'name': 'Alice'})
print(response)
AKeyError: 'name'
B"Hello, {name}!"
C"Hello, Alice!"
DTypeError: greet() missing 1 required positional argument: 'name'
Attempts:
2 left
💡 Hint
Think about how the function greet is called with the argument 'name' from the input dictionary.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a function for OpenAI functions agent in LangChain?
You want to define a function to be used by LangChain's OpenAI functions agent. Which of the following function definitions is syntactically correct and compatible?
A
def add_numbers(a: int, b: int) -> int:
    return a + b
B
def add_numbers(a, b):
return a + b
C
def add_numbers(a: int, b: int):
    print(a + b)
D
def add_numbers(a: int, b: int) -> int
    return a + b
Attempts:
2 left
💡 Hint
Check for proper indentation, type annotations, and function syntax.
state_output
advanced
2:30remaining
What is the value of 'result' after invoking the agent with nested function calls?
Consider this code using LangChain's OpenAI functions agent with two functions. What is the value of 'result' after calling agent.invoke({'x': 3, 'y': 4})?
LangChain
from langchain.agents import create_openai_functions_agent
from langchain.chat_models import ChatOpenAI

def multiply(a: int, b: int) -> int:
    return a * b

def add_and_multiply(x: int, y: int) -> int:
    sum_val = x + y
    return multiply(sum_val, 2)

llm = ChatOpenAI(temperature=0)
agent = create_openai_functions_agent(llm, [add_and_multiply, multiply])

result = agent.invoke({'x': 3, 'y': 4})
AKeyError: 'x'
B10
CTypeError due to missing argument in multiply
D14
Attempts:
2 left
💡 Hint
Trace the calls: add_and_multiply adds x and y, then calls multiply with sum_val and 2.
🔧 Debug
advanced
2:00remaining
Which option causes a runtime error when invoking the OpenAI functions agent?
Given these function definitions for an OpenAI functions agent, which option will cause a runtime error when calling agent.invoke({'name': 'Bob'})?
LangChain
from langchain.agents import create_openai_functions_agent
from langchain.chat_models import ChatOpenAI

def say_hello(name: str) -> str:
    return f"Hello, {name}!"

def say_goodbye() -> str:
    return "Goodbye!"

llm = ChatOpenAI(temperature=0)
agent = create_openai_functions_agent(llm, [say_hello, say_goodbye])
Aagent.invoke({})
Bagent.invoke({'name': 'Bob'})
Cagent.invoke({'name': 'Bob', 'extra': 123})
Dagent.invoke({'name': None})
Attempts:
2 left
💡 Hint
Check which function requires arguments and what happens if they are missing.
🧠 Conceptual
expert
2:30remaining
Which statement best describes how LangChain's OpenAI functions agent handles function calls?
Select the most accurate description of how LangChain's OpenAI functions agent processes function calls when given multiple functions.
AThe agent calls all provided functions sequentially with the same input and returns the last function's output.
BThe agent uses the LLM to decide which function to call based on the input and calls it with matching arguments automatically.
CThe agent requires manual selection of which function to call before invocation.
DThe agent only supports one function and ignores others if multiple are provided.
Attempts:
2 left
💡 Hint
Think about how the agent uses the language model to route calls.