Skip to content

Commit

Permalink
update fc
Browse files Browse the repository at this point in the history
  • Loading branch information
wangwei1237 committed Feb 6, 2024
1 parent a9945e0 commit 5e45f0e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 4 additions & 1 deletion code/langchain/utils/call_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
)
from langchain.pydantic_v1 import BaseModel

from langsmith.run_helpers import traceable

@traceable(run_type="tool") #<1>
def call_function(functions: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable]],
fc_by_llm: dict) -> str:
"""Calling the function and return the result."""
Expand All @@ -33,4 +36,4 @@ def call_function(functions: Sequence[Union[Dict[str, Any], Type[BaseModel], Cal
key: fc_args_by_llm[key] for key in func_args_keys if key in fc_args_by_llm
}
res = func(**func_args)
return res
return res
19 changes: 14 additions & 5 deletions code/test_ernie_fc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
"""

import json
import uuid

from langchain.chains import LLMChain
from langchain.chains.ernie_functions import (
create_ernie_fn_chain,
)
from langchain.chat_models import ErnieBotChat
from langchain.prompts.chat import (
from langchain_community.chat_models import QianfanChatEndpoint
from langchain_core.prompts.chat import (
ChatPromptTemplate,
)

from utils.call_function import call_function

run_id = str(uuid.uuid4())
print(run_id)


def get_current_news(location: str) -> str:
"""Get the current news based on the location.'
Expand Down Expand Up @@ -56,24 +61,28 @@ def get_current_weather(location: str, unit: str="celsius") -> str:
}
return json.dumps(weather_info)

llm = ErnieBotChat(model_name="ERNIE-Bot-4")

llm = QianfanChatEndpoint(model="ERNIE-Bot-4")

prompt = ChatPromptTemplate.from_messages(
[
("human", "{query}"),
]
)
chain = create_ernie_fn_chain([get_current_weather, get_current_news], llm, prompt, verbose=True)
res = chain.run("北京今天的新闻是什么?")
res = chain.invoke({"query": "北京今天的新闻是什么?"}, config={"metadata": {"run_id": run_id}})
print(res)
res = res["function"]

if res:
res_cf = call_function([get_current_news, get_current_weather], res)
print(res_cf)
prompt_2 = ChatPromptTemplate.from_messages(
[
("human", "从 {function} 中,我们得到如下信息:{function_res},那么 {query}"),
]
)
chain_2 = LLMChain(llm=llm, prompt=prompt_2, verbose=True)
res_2 = chain_2.run(function=res["name"], function_res=res_cf, query="北京今天的新闻是什么?")
res_2 = chain_2.invoke({"function": res["name"], "function_res": res_cf, "query": "北京今天的新闻是什么?"}, config={"metadata": {"run_id": run_id}})
print(res_2)

Binary file added images/fc_ls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions langchain_function_call.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,10 @@ print(res)
## QianfanChatEndpoint
```{#lst-fc_multi_fcs_qianfan .python code-line-numbers="true" lst-cap="使用 QianfanChatEndpoint 执行文心大模型的函数调用"}
from langchain.chat_models import QianfanChatEndpoint
from langchain.prompts import ChatPromptTemplate
ffrom langchain_community.chat_models import QianfanChatEndpoint
from langchain_core.prompts.chat import (
ChatPromptTemplate,
)
from langchain.chains.ernie_functions import (
create_ernie_fn_chain,
)
Expand Down Expand Up @@ -484,6 +486,8 @@ Human: 北京今天新闻是什么?
```{#lst-la_wx_fc_run .python include="./code/langchain/utils/call_function.py" code-line-numbers="true" lst-cap="utils.call_function.call_function()"}
```
1. 方便 LangSmith 可以追踪到函数调用,方便 DEBUG。
通过文心大模型的函数调用解决我们的问题的完整代码如 @lst-la_wx_fc_demo_all 所示。
```{#lst-la_wx_fc_demo_all .python include="./code/test_ernie_fc.py" code-line-numbers="true" lst-cap="文心大模型利用函数调用解决问题"}
Expand All @@ -507,7 +511,9 @@ Human: 从 get_current_news 中,我们得到如下信息:{"location": "\u531
根据提供的信息,`get_current_news` 返回的数据中,"北京"的新闻有两条,分别是 "I have a Book.""It's a nice day, today."。所以,北京今天的新闻包括这两条信息。
```
整个函数调用的的整个过程如 @fig-fc_ls 所示。
![函数调用的 Trace 图](./images/fc_ls.png){#fig-fc_ls}
## 参考文献
[^1]: [Function calling and other API updates](https://openai.com/blog/function-calling-and-other-api-updates)
Expand Down

0 comments on commit 5e45f0e

Please sign in to comment.