跳至主要内容

Rivet集成入门指南

欢迎阅读Rivet集成入门指南!目前我们仅支持通过Node.js使用@ironclad/rivet-node包进行集成。

安装

使用您喜欢的包管理器进行安装:

使用Yarn安装:

yarn add @ironclad/rivet-node

入门指南

安装Rivet后,您可以将其导入到您的应用程序中:

import * as Rivet from '@ironclad/rivet-node';

使用 runGraphInFile

开始使用Rivet的最简单方法是使用runGraphInFile函数。该函数允许您执行在Rivet项目文件中定义的图形。

runGraphInFile 参数

runGraphInFile 函数接收两个参数:

  1. path: 表示Rivet项目文件路径的字符串。
  2. options: 一个类型为RunGraphOptions的对象。

RunGraphOptions

RunGraphOptions 类型用于向 runGraphInFile 函数传递各种参数。其结构如下:

export type RunGraphOptions = {
graph: string;
inputs?: Record<string, LooseDataValue>;
context?: Record<string, LooseDataValue>;
remoteDebugger?: RivetDebuggerServer;
nativeApi?: NativeApi;
externalFunctions?: {
[key: string]: ExternalFunction;
};
onUserEvent?: {
[key: string]: (data: DataValue | undefined) => void;
};
abortSignal?: AbortSignal;
} & {
[P in keyof ProcessEvents as `on${PascalCase<P>}`]?: (params: ProcessEvents[P]) => void;
} & Settings;

让我们分解一下重要字段:

  • graph: 指定您要运行的graph。可以是graph的ID或显示名称。
  • inputs: 指定图的输入值。这些可以是普通的JavaScript值如"foo",或是{type: 'string', value: 'foo'}对象。
  • context: 类似于inputs,但这些值对每个图表和子图表都可用。
  • externalFunctions: 这是定义可以从Rivet图中调用的集成点的方式。
  • openAiKey: 您的OpenAI API密钥。如果您在图中使用任何Chat节点,则此项为必填。
  • openAiOrganization: 如果您使用的是非默认的OpenAI组织,可以在此处指定您的组织。

示例代码

以下是使用runGraphInFile的基本示例:

import { runGraphInFile, DataValue } from '@ironclad/rivet-node';

await runGraphInFile('./myProject.rivet', {
graph: 'My Graph Name',
inputs: {
myInput: 'hello world',
},
context: {
myContext: 'global value',
},
externalFunctions: {
helloWorld: async (...args: unknown[]): Promise<DataValue> => {
return {
type: 'string',
value: 'hello world',
};
},
},
onUserEvent: {
myEvent: (data: DataValue): Promise<void> => {
console.log(data);
},
},
openAiKey: 'my-openai-key',
openAiOrganization: 'my-organization',
});

远程调试

有关如何设置远程调试器的更多信息,请参阅Remote Debugging页面。

另请参阅