-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.ts
More file actions
144 lines (132 loc) · 4.93 KB
/
tools.ts
File metadata and controls
144 lines (132 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { TavilySearchResults } from "@langchain/community/tools/tavily_search"
import { tool } from "@langchain/core/tools"
import { z } from "zod"
import { CryptoInfo, CryptoPriceConversion, CryptoQuotes } from "./types"
export async function callCoinMarketCapAPI<Output extends Record<string, any> = Record<string, any>>(
fields: { endpoint: string, params: Record<string, string> }): Promise<Output> {
if (!process.env.COINMARKETCAP_API_KEY) {
throw new Error("COINMARKETCAP_API_KEY is not set")
}
const baseURL = "https://pro-api.coinmarketcap.com/v1"
const queryParams = new URLSearchParams(fields.params).toString()
const url = `${baseURL}${fields.endpoint}?${queryParams}`
const response = await fetch(url, {
method: "GET",
headers: {
"X-CMC_PRO_API_KEY": process.env.COINMARKETCAP_API_KEY
}
})
if (!response.ok) {
let res: string
try {
res = JSON.stringify(await response.json(), null, 2)
} catch (_) {
try {
res = await response.text()
} catch (_) {
res = response.statusText
}
}
throw new Error(
`Failed to fetch data from ${fields.endpoint}.\nResponse: ${res}`
)
}
const data = await response.json()
return data
}
const cryptoInfo = tool(async (input) => {
console.log("*** cryptocurrencyInfo ***")
try {
const data = await callCoinMarketCapAPI<CryptoInfo>({
endpoint: "/cryptocurrency/info",
params: {
symbol: input.symbol
}
})
return JSON.stringify(data, null)
} catch (e: any) {
console.warn("Error fetching cryptocurrency info", e.message)
return `An error occured while fetching cryptocurrency info, ${e.message}`
}
}, {
name: "cryptocurrency_info",
description: `Fetches all static metadata available for the current cryptocurrency. This information
includes details like logo, description, official website URL, social links, and links
to a cryptocurrency's technical documentation
`,
schema: z.object({
symbol: z
.string()
.describe("Cryptocurrency symbol. For example 'BTC'")
})
})
const cryptoQuotes = tool(async (input) => {
console.log("*** cryptocurrencyQuotes ***")
try {
const data = await callCoinMarketCapAPI<CryptoQuotes>({
endpoint: "/cryptocurrency/quotes/latest",
params: {
symbol: input.symbol,
convert: input.convert
}
})
return JSON.stringify(data, null)
} catch (e: any) {
console.warn("Error fetching cryptocurrency quotes", e.message)
return `An error occured while fetching cryptocurrency quotes, ${e.message}`
}
}, {
name: "cryptocurrency_quotes",
description: `Fetches the latest market quotes for specified cryptocurrencies. It provides detailed metrics like current price, 24-hour
trading volume, market cap, and percentage price changes over various time frames. Use the "convert" option to return market value in the same call.`,
schema: z.object({
symbol: z
.string()
.describe("Cryptocurrency symbol. For example 'BTC'"),
convert: z
.string()
.describe("Target currency to return the quote data. For example 'USD'")
}),
})
const cryptoPriceConversionSchema = z.object({
symbol: z
.string()
.describe("Cryptocurrency symbol. For example 'BTC'"),
convert: z
.string()
.describe("Trget currency to return the quote data. For example 'USD'"),
amount: z
.string()
.describe("Amount of currency to convert. For Example: 10.43")
})
export type priceConversion = z.infer<typeof cryptoPriceConversionSchema>
const cryptoPriceConversion = tool(async (input) => {
console.log("*** cryptocurrencyPriceConversion ***")
try {
const data = await callCoinMarketCapAPI<CryptoPriceConversion>({
endpoint: "/tools/price-conversion",
params: {
symbol: input.symbol,
convert: input.convert,
amount: input.amount
}
})
return JSON.stringify(data, null)
} catch (e: any) {
console.warn("Error fetching cryptocurrency price conversion", e.message)
return `An error occured while fetching cryptocurrency price conversion, ${e.message}`
}
}, {
name: "cryptocurrency_price_conversion",
description: "Converts an amount of one cryptocurrency or fiat currency into one or more different currencies utilizing the latest market rate for each currency",
schema: cryptoPriceConversionSchema,
})
export const webSearchTool = new TavilySearchResults({
maxResults: 2
})
export const ALL_TOOLS = [
cryptoInfo,
cryptoQuotes,
cryptoPriceConversion,
webSearchTool
]