add tool call example
This commit is contained in:
@@ -147,4 +147,71 @@ mujianSdk.init().then(() => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
$("#tool-call-button").click(async () => {
|
||||
$("#tool-call-result").html("");
|
||||
|
||||
const weatherTool = {
|
||||
type: "function",
|
||||
function: {
|
||||
name: "get_weather",
|
||||
description: "Get the current weather in a location",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
location: {
|
||||
type: "string",
|
||||
description: "The city and state, e.g. San Francisco, CA",
|
||||
},
|
||||
unit: {
|
||||
type: "string",
|
||||
enum: ["celsius", "fahrenheit"],
|
||||
description: "The unit of temperature, e.g. celsius, fahrenheit",
|
||||
default: "celsius",
|
||||
},
|
||||
},
|
||||
required: ["location"],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const get_weather = async (location, unit) => {
|
||||
console.log("get_weather", location, unit);
|
||||
return {
|
||||
weather: "sunny",
|
||||
temperature: 20,
|
||||
unit: unit,
|
||||
};
|
||||
};
|
||||
|
||||
const TOOL_MAPPING = {
|
||||
get_weather,
|
||||
};
|
||||
|
||||
const res = await mujianSdk.ai.openai.chat.completions.create({
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: "You are a helpful assistant.",
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: "What is the weather in San Francisco? in fahrenheit",
|
||||
},
|
||||
],
|
||||
tools: [weatherTool],
|
||||
tool_choice: "auto",
|
||||
max_output_tokens: 9000,
|
||||
});
|
||||
console.log("tool call success", res);
|
||||
$("#tool-call-result").html(JSON.stringify(res));
|
||||
const msg = res.choices[0].message;
|
||||
|
||||
for (const toolCall of msg?.tool_calls || []) {
|
||||
const toolName = toolCall.function.name;
|
||||
const { location, unit } = JSON.parse(toolCall.function.arguments);
|
||||
const toolResponse = await TOOL_MAPPING[toolName](location, unit);
|
||||
console.log("tool response", toolResponse);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -136,6 +136,14 @@
|
||||
</h4>
|
||||
<div id="image-generation-stream-result"></div>
|
||||
|
||||
<h4>
|
||||
Tool call 示例(非流式)
|
||||
<button class="ui-button ui-widget ui-corner-all" id="tool-call-button">
|
||||
触发Tool call
|
||||
</button>
|
||||
</h4>
|
||||
<div id="tool-call-result"></div>
|
||||
|
||||
<!-- 幕间官方SDK -->
|
||||
<script src="https://npm.onmicrosoft.cn/@mujian/js-sdk@0.0.6-beta.45/dist/umd/index.js"></script>
|
||||
<!-- 第三方库:JQuery - 主要用于提供便利地操作页面元素的函数 -->
|
||||
|
||||
Reference in New Issue
Block a user