docs: update README.md

This commit is contained in:
Alex Yang
2024-04-13 20:58:11 -05:00
parent cdd59f5a21
commit b0ad672b8e
+43 -1
View File
@@ -9,7 +9,49 @@
## Usage
### Node.js
### In your code
```ts
// @file: index.llama.ts
export function getWeather(city: string) {
return `The weather in ${city} is sunny.`
}
export function getTemperature(city: string) {
return `The temperature in ${city} is 25°C.`
}
export function getCurrentCity() {
return 'New York'
}
// @file: app.ts
import Tools from './index.llama'
import { registerTools, convertTools } from '@llamaindex/tool'
// Register tools on top level
registerTools(Tools)
import { OpenAI } from 'openai'
const openai = new OpenAI()
openai.chat.completions.create({
messages: [
{
role: 'user',
content: 'What is the weather in the current city?'
}
],
tools: convertTools('openai')
})
// or you can use llamaindex openai agent
import { OpenAIAgent } from 'llamaindex'
const agent = new OpenAIAgent({
tools: convertTools('llamaindex')
})
const { response } = await agent.chat({
message: 'What is the temperature in the current city?'
})
console.log('Response:', response)
```
### Run with Node.js
```shell
node --import tsx --import @llamaindex/tool/register ./app.ts