3.6.4 • Published 7 months ago
@celljs/ai-openai v3.6.4
Cell - AI OpenAI Component
概览
AI OpenAI 模块是一个用于与 OpenAI API 交互的库,提供了生成聊天响应和嵌入向量的功能。通过简单易用的 API 接口,支持消息的创建、请求的发送和响应的处理。是 @celljs/ai-core 模块中所有模型服务接口抽象的一种实现。
特性
- 生成聊天响应
- 生成嵌入向量
- 支持流式响应
- 支持多种模型参数配置
安装
使用 npm 安装 AI OpenAI 模块:
npm install @celljs/ai-openai或者使用 yarn:
yarn add @celljs/ai-openai快速开始
以下是一个简单的示例,展示如何使用 AI OpenAI 模块生成聊天响应和嵌入向量:
import { AssistantMessage, PromptTemplate } from '@celljs/ai-core';
import { OpenAIChatModel, OpenAIModel, } from '@celljs/ai-openai';
import { Component Autowired } from '@celljs/core';
@Component()
export class OpenAIDemo {
@Autowired(OpenAIChatModel)
private openAIChatModel: OpenAIChatModel;
@Autowired(OpenAIEmbeddingModel)
private openAIEmbeddingModel: OpenAIEmbeddingModel;
@Autowired(PromptTemplate)
private promptTemplate: PromptTemplate;
/**
* Chat with OpenAI
*/
async chat() {
const prompt = await this.promptTemplate.create(
'Hello {name}',
{
chatOptions: { model: OpenAIModel.O1_MINI },
variables: { name: 'OpenAI' }
}
);
const response = await this.openAIChatModel.call(prompt);
console.log(response.result.output);
}
/**
* Stream chat response
*/
async stream() {
const prompt = await this.promptTemplate.create(
'Hello {name}',
{
chatOptions: { model: OpenAIModel.O1_MINI },
variables: { name: 'OpenAI' }
}
);
const response$ = await this.openAIChatModel.stream(prompt);
response$.subscribe({
next: response => console.log(response.result.output),
complete: () => console.log('Chat completed!')
});
}
/**
* Embed text to vector
*/
async embed() {
const response = await this.openAIEmbeddingModel.call({
inputs: ['text to embed'],
options: { model: OpenAIModel.TEXT_EMBEDDING_3_LARGE }
});
console.log(response.result.embeddings);
}
}许可证
本项目采用 MIT 许可证。
3.6.4
7 months ago
3.6.3
7 months ago
3.6.2
7 months ago
3.6.1
8 months ago
3.6.0
8 months ago
3.5.14
8 months ago
3.5.13
8 months ago
3.5.12
8 months ago
3.5.11
8 months ago
3.5.10
8 months ago
3.5.9
8 months ago
3.5.8
8 months ago
3.5.7
9 months ago
3.5.6
9 months ago
3.5.5
9 months ago
3.5.4
9 months ago
3.5.3
9 months ago
3.5.2
9 months ago
3.5.1
9 months ago
3.4.4
9 months ago