Replies: 1 comment
-
Hello @khw11044! I'm here to help you with any questions or issues you have while you wait for a human maintainer. Let’s tackle this together! To build a custom dataset for fine-tuning a local small LLM with function calling to create a Langchain agent that follows the same flow as OpenAI's Langchain agent, you can follow these steps:
This process allows you to create a custom dataset and fine-tune a model to follow the same flow as OpenAI's Langchain agent, while also supporting local LLMs [1][2][3]. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
Beta Was this translation helpful? Give feedback.
-
"I built a Langchain agent using OpenAI, and the agent effectively utilizes the tools and provides the desired results according to my intentions. Now, I want to build a Langchain agent using an open-source local LLM. When I built an agent using llama3.2, it doesn't use the tools well. Therefore, I have been diligently researching under the title 'fine tuning llm for function calling.' However, when I actually apply this to the Langchain agent I built, it doesn't work well."
for example,
from langchain_ollama import ChatOllama
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain.agents import AgentExecutor
from typing import List
from langchain.agents import tool
from dotenv import load_dotenv
API 키 정보 로드
load_dotenv()
@tool
def add(xy_pairs: List[tuple]) -> List[dict]:
"""Add two integers together.
"""
results = []
for x, y in xy_pairs:
result = {
f"{x}+{y}": x + y,
}
results.append(result)
return results
@tool
def multiply(xy_pairs: List[tuple]) -> List[dict]:
"""Multiply two integers together.
"""
results = []
for x, y in xy_pairs:
result = {
f"{x}*{y}": x * y,
}
results.append(result)
return results
tools = [add, multiply]
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant. "
),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
]
)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.6)
llm_with_tools = llm.bind_tools(tools)
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
)
query = "What is 3 * 12?"
result = agent_executor.invoke({"input": query})
log is
Invoking:
multiply
with{'xy_pairs': [[3, 12]]}
[{'3*12': 36}]3 * 12 is 36.
log_history = []
stream_iterator = agent_executor.stream({"input": query})
for step in stream_iterator:
log_history.append(step)
log_history
You can see results like the ones below
[{'actions': [ToolAgentAction(tool='multiply', tool_input={'xy_pairs': [[3, 12]]}, log="\nInvoking: multiply with ~
'messages': [AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call ~
{'steps': [AgentStep(action=ToolAgentAction(tool='multiply', tool_input={'xy_pairs': [[3, 12]]}, log="\nInvoking ~
'messages': [FunctionMessage(content='[{"3*12": 36}]', additional_kwargs={}, response ~
{'output': 'The result of (3 \times 12) is (36).',
'messages': [AIMessage(content='The result of (3 \times ~
However, when using a fine-tuned LLM, I have seen the following results.
Entering new AgentExecutor chain...
{"type":"function","function":{"name":"multiply","arguments":[{"x":3, "y":12}]}}
The result of the multiplication operation between 3 and 12 is 36.
Finished chain.
log_history
{'output': '{"type":"function","function":{"name":"multiply","arguments":[{"x":3, "y":12}]}}\nThe result of the multiplication operation between 3 and 12 is 36.',
'messages': [AIMessage(content='{"type":"function","function":{"name":"multiply","arguments":[{"x":3, "y":12}]}}\nThe result of the multiplication operation between 3 and 12 is 36.', additional_kwargs={}, response_metadata={})]}]
I want to know in detail about building a dataset for a Langchain agent from scratch. I want to fine-tune a local small LLM so that it can follow the same flow as the trajectories of OpenAI's Langchain agent as much as possible. How should I do this?
Beta Was this translation helpful? Give feedback.
All reactions