Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • mirrored_repos/machinelearning/run-llama/tool
1 result
Show changes
// import { getCurrentStreamableUI } from '@/context'
// import { Spinner } from '@/components/spinner'
// import { LocationCard } from '@/components/location-card'
//
// export async function getWeather (
// location: string
// ) {
// console.log('Getting the weather for', location)
// const ui = getCurrentStreamableUI()
// if (ui) {
// ui.update(
// <div>
// Getting the weather for {location}...
// </div>
// )
// }
// await new Promise(resolve => setTimeout(resolve, 1000))
// if (ui) {
// ui.update(
// <div>
// The weather in {location} is sunny!
// </div>
// )
// }
// return 'Sunny'
// }
//
// /**
// * Get user's current location
// *
// * user's current location, if it's null, return 'San Francisco, CA'
// */
// export async function getCurrentLocation () {
// console.log('Getting current location')
// const ui = getCurrentStreamableUI()
// if (ui) {
// ui.append(
// <div>
// Getting current location <Spinner/>
// </div>
// )
// }
// await new Promise(resolve => setTimeout(resolve, 1000))
// // if (ui) {
// // ui.update(<LocationCard/>)
// // }
// return null
// }
import { getCurrentStreamableUI } from '@/context'
export async function getMyUserID () {
const ui = getCurrentStreamableUI()!
ui.update('Getting user ID...')
await new Promise(resolve => setTimeout(resolve, 2000))
return '12345'
}
export async function showUserInfo (
userId: string
) {
const ui = getCurrentStreamableUI()!
ui.update('Getting user info...')
await new Promise(resolve => setTimeout(resolve, 2000))
ui.update(
<div>
User ID: {userId}
<br/>
Name: John Doe
</div>
)
return `User ID: ${userId}\nName: John Doe\nEmail: alex@gmail.com\nPhone: 123-456-7890\nAddress: 123 Main St\nCity: San Francisco\nState: CA\nZip: 94105\nCountry: USA\n`
}
export function getWeather (
address: string
) {
return `The weather in ${address} is sunny!`
}
\ No newline at end of file
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
},
"forceConsistentCasingInFileNames": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
......@@ -2,9 +2,5 @@
"name": "@llamaindex/root",
"type": "module",
"version": "1.0.0",
"scripts": {
"build": "bunchee",
"dev:01_node": "node --import tsx --import ./src/register.ts ./examples/01_node/src/index.ts"
},
"private": true
}
{
"name": "@llamaindex/tool",
"type": "module",
"version": "1.0.0",
"version": "0.0.1",
"description": "",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./next": {
"types": "./dist/next.d.ts",
"import": "./dist/next.js",
"require": "./dist/next.cjs",
"default": "./dist/next.js"
},
"./loader": "./dist/loader.js",
"./register": "./dist/register.js"
},
......@@ -26,14 +34,17 @@
"openai": "^4.33.0",
"ts-json-schema-generator": "^1.5.1",
"typedoc": "^0.25.13",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"unplugin": "^1.10.1"
},
"devDependencies": {
"@swc/types": "^0.1.6",
"@types/json-schema": "^7.0.15",
"bunchee": "^5.1.2",
"llamaindex": "^0.2.10",
"llamaindex": "^0.3.0",
"next": "14.2.3",
"tsx": "^4.7.2",
"vitest": "^1.5.0"
"vitest": "^1.5.0",
"webpack": "^5.91.0"
}
}
......@@ -48,7 +48,7 @@ const llamaindexToolsAtom = atom<BaseToolWithCall[]>(get => {
arr[idx] = input[name]
return arr
}, [] as unknown[])
const fn = fns[metadata.name]
const fn = fns[metadata.name] ?? info.originalFunction;
if (!fn) {
throw new Error(`Cannot find function to call: ${metadata.name}`)
}
......
......@@ -2,6 +2,7 @@ import { atom, createStore } from 'jotai/vanilla'
import type { ToolMetadata } from 'llamaindex'
export type Info = {
originalFunction?: (...args: any[]) => any
parameterMapping: Record<string, number>
}
......
import { createWebpackPlugin } from 'unplugin'
import { unpluginFactory } from './plugin'
import type { NextConfig } from 'next'
const webpackPlugin = createWebpackPlugin(unpluginFactory)
export function withNext (config: NextConfig) {
return {
...config,
webpack: (config: any) => {
config.plugins.push(webpackPlugin())
return config
}
}
}
\ No newline at end of file
import { createWebpackPlugin, type UnpluginFactory } from 'unplugin'
import { createUnplugin } from 'unplugin'
import { parseRoot } from './compiler'
import type { Compiler } from 'webpack'
import type {
JSONSchema7,
JSONSchema7Definition,
JSONSchema7TypeName
} from 'json-schema'
import type { Info } from './internal'
import type { ToolMetadata } from 'llamaindex'
import { parse } from '@swc/core'
import type { ExportDeclaration, ImportDeclaration } from '@swc/types'
export interface Options {
}
const roots: string[] = []
const name = 'llama-index-tool'
const isToolRegex = /tool\.tsx?$/
export const unpluginFactory: UnpluginFactory<Options | undefined> = options => ({
name,
transformInclude (id) {
const isTool = isToolRegex.test(id)
if (isTool) {
roots.push(id)
}
return isTool;
},
async transform (code, id) {
if (roots.includes(id)) {
const json = await parseRoot(id)
const children = json.children
if (Array.isArray(children)) {
const schema = {
type: 'object',
properties: {} as {
[key: string]: JSONSchema7Definition
},
additionalItems: false,
required: [] as string[]
} satisfies JSONSchema7
const info: Info = {
originalFunction: undefined,
parameterMapping: {}
}
children.forEach(child => {
info.originalFunction = child.name as any;
const metadata: ToolMetadata = {
name: child.name,
description: '',
parameters: schema
}
child.signatures?.forEach(signature => {
const description = signature.comment?.summary.map(x => x.text).
join('\n')
if (description) {
metadata.description += description
}
signature.parameters?.map((parameter, idx) => {
if (parameter.type?.type === 'intrinsic') {
// parameter.type.name
schema.properties[parameter.name as string] = {
type: parameter.type.name as JSONSchema7TypeName,
description: parameter.comment?.summary.map(x => x.text).
join('\n')
} as JSONSchema7Definition
schema.required.push(parameter.name as string)
info.parameterMapping[parameter.name as string] = idx
}
})
})
const infoJSON = JSON.stringify(info)
// remove quotes from `originalFunction` value
.replace(/"originalFunction":"(.*?)"/g, '"originalFunction":$1')
code = `injectMetadata(${JSON.stringify(
metadata)}, ${infoJSON});\n` +
code
})
}
if (!/^import\s+{\sinjectMetadata\s}\s+from\s+['"]@llamaindex\/tool['"]/.test(code)) {
code = `import {injectMetadata} from '@llamaindex/tool';\n${code}`
}
return {
code,
map: null
}
}
},
webpack(compiler: Compiler) {
if (compiler.options.mode === 'development') {
compiler.hooks.done.tap(name, async (state) => {
if (state.hasErrors()) {
return
}
})
}
},
})
export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)
export default unplugin
This diff is collapsed.