《扣子开发AI Agent智能体应用(人工智能技术丛书)》(宋立桓,王东健,陈铭毅,程东升)【摘要 书评 试读】- 京东图书
3.3节 开发自定义插件案例,自动生成的代码运行会出错,需要改为作者提供的代码(参看书中的图片),操作起来不方便,可以直接到本文后面复制这段代码。
代码直接使用作者提供的,放在下面。有编程知识的读者,可以对比一下自动生成的代码与作者提供的代码有什么区别,从而更好地理解这里代码的作用。
import { Args } from '@/runtime';
import { Input, Output } from "@/typings/search_stock_prices/search_stock_prices";
import axios from 'axios';
export async function handler({ input, logger }: Args<Input>): Promise<Output> {
const code = input.code;
const apiKey = 'YOUR_ALPHA_VANTAGE_API_KEY';
const url = `https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${code}&apikey=${apiKey}`;
try {
const response = await axios.get(url);
const data = response.data['Global Quote'];
return {
code: code,
price: data['05. price'],
};
} catch (error) {
logger.error(`Error fetching stock price for ${code}: ${error}`);
return {
code: code,
price: null,
};
}
}
在预览与调试窗口的对话框中输入“MSFT 股票价格是多少?”,如下图所示。
本案例顺利完成。