Closed imyizhang closed 3 weeks ago
请维护README文档
更新如下
比如将 mulitply
乘法函数定义传入 ChatSparkLLM
import json
from sparkai.llm.llm import ChatSparkLLM, ChunkPrintHandler
from sparkai.core.messages import ChatMessage
from sparkai.core.utils.function_calling import convert_to_openai_tool
def multiply(a: int, b :int) -> int:
"""你是一个乘法计算器,可以帮我计算两个数的乘积,例如:计算1乘1等于几或计算1*1等于几
Args:
a: 输入a
b: 输入b
Return:
返回 a*b 结果
"""
return a * b
def test_function_calling(text: str = "帮我算下 12乘以12"):
spark = ChatSparkLLM(
spark_api_url=SPARKAI_URL,
spark_app_id=SPARKAI_APP_ID,
spark_api_key=SPARKAI_API_KEY,
spark_api_secret=SPARKAI_API_SECRET,
spark_llm_domain=SPARKAI_DOMAIN,
streaming=False,
request_timeout=30,
temperature=0.5,
top_k=4,
max_tokens=4096,
)
messages = [ChatMessage(
role="user",
content=text,
)]
handler = ChunkPrintHandler()
function_definition = [convert_to_openai_tool(multiply)]
print(json.dumps(convert_to_openai_tool(multiply), indent=4, ensure_ascii=False))
a = spark.generate(
[messages],
callbacks=[handler],
unction_definition=function_definition,
)
print(a)
print(a.generations[0][0].text)
print(a.llm_output)
# 测试function calling
test_function_calling()
得到输出
{
"type": "function",
"function": {
"name": "multiply",
"description": "你是一个乘法计算器,可以帮我计算两个数的乘积,例如:计算1乘1等于几或计算1*1等于几\nArgs:\n a: 输入a\n b: 输入b\nReturn:\n 返回 a*b 结果",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "integer"
},
"b": {
"type": "integer"
}
},
"required": [
"a",
"b"
]
}
}
}
generations=[[ChatGeneration(text='要计算$12$乘以$12$,即求$12\\times 12$ $=144$.\n\n所以,$12$乘以$12$等于$144$.', message=AIMessage(content='要计算$12$乘以$12$,即求$12\\times 12$ $=144$.\n\n所以,$12$乘以$12$等于$144$.'))]] llm_output={'token_usage': {'question_tokens': 9, 'prompt_tokens': 9, 'completion_tokens': 47, 'total_tokens': 56}} run=[RunInfo(run_id=UUID('795b5e3d-4065-40a6-8f89-6fa649cba4fa'))]
要计算$12$乘以$12$,即求$12\times 12$ $=144$.
所以,$12$乘以$12$等于$144$.
{'token_usage': {'question_tokens': 9, 'prompt_tokens': 9, 'completion_tokens': 47, 'total_tokens': 56}}
根据README测试function calling接口,得到与文档相同的返回结果,即
a.generations[0][0].text
为''
。