Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
toandev95 committed Oct 23, 2024
1 parent 20bbfaa commit 93593b5
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 5 deletions.
4 changes: 3 additions & 1 deletion components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export function Chat({ id, className, session, missingKeys }: ChatProps) {

<div
className={cn(
messages.length ? 'pb-[200px] pt-4 md:pt-6' : 'pb-[200px] pt-0',
messages.length
? 'pb-[100px] md:pb-[200px] pt-4 md:pt-6'
: 'pb-[200px] pt-0',
className
)}
ref={messagesRef}
Expand Down
90 changes: 90 additions & 0 deletions components/tradingview/forex-rates.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use client'

import React, { memo, useEffect, useRef } from 'react'

export function ForexRates({ props: symbol }: { props: string }) {
const container = useRef<HTMLDivElement>(null)

useEffect(() => {
if (!container.current) return
const script = document.createElement('script')
script.src =
'https://s3.tradingview.com/external-embedding/embed-widget-symbol-overview.js'
script.type = 'text/javascript'
script.async = true
script.innerHTML = `
{
"symbols": [
[
"${symbol}"
]
],
"chartOnly": false,
"width": "100%",
"height": "100%",
"locale": "vi_VN",
"colorTheme": "light",
"autosize": true,
"showVolume": false,
"showMA": false,
"hideDateRanges": false,
"hideMarketStatus": false,
"hideSymbolLogo": false,
"scalePosition": "right",
"scaleMode": "Normal",
"fontFamily": "-apple-system, BlinkMacSystemFont, Trebuchet MS, Roboto, Ubuntu, sans-serif",
"fontSize": "10",
"noTimeScale": false,
"valuesTracking": "1",
"changeMode": "price-and-percent",
"chartType": "area",
"maLineColor": "#2962FF",
"maLineWidth": 1,
"maLength": 9,
"backgroundColor": "rgba(255, 255, 255, 0)",
"lineWidth": 2,
"lineType": 0,
"dateRanges": [
"1d|1",
"1m|30",
"3m|60",
"12m|1D",
"60m|1W",
"all|1M"
]
}`

container.current.appendChild(script)

return () => {
if (container.current) {
const scriptElement = container.current.querySelector('script')
if (scriptElement) {
// eslint-disable-next-line react-hooks/exhaustive-deps
container.current.removeChild(scriptElement)
}
}
}
}, [symbol])

return (
<div style={{ height: '500px' }}>
<div className="tradingview-widget-container" ref={container}>
<div className="tradingview-widget-container__widget"></div>
<div className="tradingview-widget-copyright">
<a
href="https://www.tradingview.com/"
rel="noopener nofollow"
target="_blank"
>
<span className="">
Theo dõi tất cả các thị trường trên TradingView
</span>
</a>
</div>
</div>
</div>
)
}

export default memo(ForexRates)
75 changes: 71 additions & 4 deletions lib/chat/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { z } from 'zod'
import { BotCard, BotMessage } from '@/components/stocks/message'
import { SpinnerMessage } from '@/components/stocks/message'
import { ETFHeatmap } from '@/components/tradingview/etf-heatmap'
import ForexRates from '@/components/tradingview/forex-rates'
import { MarketHeatmap } from '@/components/tradingview/market-heatmap'
import { MarketOverview } from '@/components/tradingview/market-overview'
import { MarketTrending } from '@/components/tradingview/market-trending'
Expand Down Expand Up @@ -72,7 +73,7 @@ async function generateCaption(

const captionSystemMessage =
`\
You are a stock market conversation bot. You can provide the user information about stocks include prices and charts in the UI. You do not have access to any information and should only provide information by calling functions.
You are a market conversation bot. You can provide the user information about stocks, forex, and other financial instruments including prices and charts in the UI. You do not have access to any information and should only provide information by calling functions.
These are the tools you have available:
1. showStockFinancials
Expand Down Expand Up @@ -102,6 +103,9 @@ This tool shows the daily top trending stocks including the top five gaining, lo
9. showETFHeatmap
This tool shows a heatmap of today's ETF market performance across sectors and asset classes.
10. showForexRates
This tool shows the exchange rates of forex pairs.
You have just called a tool (` +
toolName +
Expand Down Expand Up @@ -199,7 +203,7 @@ async function submitUserMessage(content: string) {
initial: <SpinnerMessage />,
maxRetries: 1,
system: `\
You are a stock market conversation bot. You can provide the user information about stocks include prices and charts in the UI. You do not have access to any information and should only provide information by calling functions.
You are a market conversation bot. You can provide the user information about stocks, forex, and other financial instruments including prices and charts in the UI. You do not have access to any information and should only provide information by calling functions.
### Cryptocurrency Tickers
For any cryptocurrency, append "USD" at the end of the ticker when using functions. For instance, "DOGE" should be "DOGEUSD".
Expand All @@ -214,8 +218,8 @@ Assistant (you): { "tool_call": { "id": "pending", "type": "function", "function
Example 2:
User: What is the price of AAPL?
Assistant (you): { "tool_call": { "id": "pending", "type": "function", "function": { "name": "showStockPrice" }, "parameters": { "symbol": "AAPL" } } }
User: What is the exchange rate of Gold?
Assistant (you): { "tool_call": { "id": "pending", "type": "function", "function": { "name": "showForexRates" }, "parameters": { "pair": "XAUUSD" } } }
`,
messages: [
...aiState.get().messages.map((message: any) => ({
Expand Down Expand Up @@ -809,6 +813,69 @@ Assistant (you): { "tool_call": { "id": "pending", "type": "function", "function
</BotCard>
)
}
},
showForexRates: {
description:
'Show the exchange rates of forex pairs. Use this to show the exchange rates to the user.',
parameters: z.object({
pair: z
.string()
.describe('The forex pair symbol. e.g. EUR/USD, GBP/JPY.')
}),
generate: async function* ({ pair }) {
yield (
<BotCard>
<></>
</BotCard>
)

const toolCallId = nanoid()

aiState.done({
...aiState.get(),
messages: [
...aiState.get().messages,
{
id: nanoid(),
role: 'assistant',
content: [
{
type: 'tool-call',
toolName: 'showForexRates',
toolCallId,
args: { pair }
}
]
},
{
id: nanoid(),
role: 'tool',
content: [
{
type: 'tool-result',
toolName: 'showForexRates',
toolCallId,
result: { pair }
}
]
}
]
})

const caption = await generateCaption(
pair,
[],
'showForexRates',
aiState
)

return (
<BotCard>
<ForexRates props={pair} />
{caption}
</BotCard>
)
}
}
}
})
Expand Down

0 comments on commit 93593b5

Please sign in to comment.