@tcsk-ai/core
v0.0.2
Published
Core utilities and types for TCSK AI SDK
Downloads
4
Readme
@tcsk-ai/core
TCSK AI SDK 的核心包,提供与TCSK GPT API交互的基础功能。
安装
npm install @tcsk-ai/core快速开始
基本使用
import { createDefaultTCSKClient, createUserMessage } from '@tcsk-ai/core'
// 创建客户端
const client = createDefaultTCSKClient(
'your-username',
'task-id-123',
'azure',
'gpt-35-turbo'
)
// 发送聊天请求
const response = await client.chat([
createUserMessage('你好,请用幽默的语气回复')
])
console.log(response.result.content)流式聊天
import { createTCSKClient, createUserMessage } from '@tcsk-ai/core'
const client = createTCSKClient({
username: 'your-username',
taskId: 'task-id-123',
companyCode: 'azure',
model: 'gpt-35-turbo',
source: 'prompt',
timeout: 60
})
// 流式聊天
const stream = await client.chatStream([
createUserMessage('请写一首关于春天的诗')
])
for await (const chunk of stream) {
if (chunk.delta.content) {
process.stdout.write(chunk.delta.content)
}
if (chunk.done) {
console.log('\n总Token数:', chunk.usage?.totalTokens)
}
}高级配置
import { createSystemMessage, createUserMessage, TCSKHttpClient } from '@tcsk-ai/core'
const client = new TCSKHttpClient({
username: 'your-username',
taskId: 'task-id-123',
companyCode: 'azure',
model: 'gpt-35-turbo',
modelMethod: 'chat_completions',
baseURL: 'https://gptapi.tcshuke.com', // 自定义API地址
source: 'custom-source',
timeout: 120,
headers: {
'X-Custom-Header': 'value'
}
})
const response = await client.chat([
createSystemMessage('你是一个专业的技术顾问'),
createUserMessage('请解释什么是微服务架构')
], {
temperature: 0.7,
maxTokens: 2048,
promptParams: {
promptId: 'tech-consultant',
source: 'knowledge-base',
templateVersion: 'v1.0'
}
})API 参考
接口定义
TCSKModelConfig
interface TCSKModelConfig {
username: string // 用户账户名
source?: string // 来源(获取token时使用)
baseURL?: string // API基础地址
accessToken?: string // 访问令牌(可选,会自动获取)
expirationTime?: number // 令牌过期时间
taskId: string // 任务ID
companyCode: string // 公司代码
model: string // 模型名称
modelMethod?: string // 模型方法
timeout?: number // 请求超时时间(秒)
headers?: Record<string, string> // 自定义请求头
}TCSKChatMessage
interface TCSKChatMessage {
role: 'system' | 'user' | 'assistant' // 消息角色
content: string // 消息内容
}TCSKChatOptions
interface TCSKChatOptions {
temperature?: number // 温度参数,控制输出随机性
maxTokens?: number // 最大输出令牌数
stream?: boolean // 流式输出
detailParams?: Record<string, any> // 详细参数
promptParams?: { // 提示词参数
promptId?: string
source?: string
templateVersion?: string
returnDataFlag?: string
dialogueId?: number
questionId?: string
logType?: string
directBack?: Record<string, any>
}
}工具函数
消息创建
import {
createAssistantMessage,
createSystemMessage,
createUserMessage
} from '@tcsk-ai/core'
const systemMsg = createSystemMessage('你是一个助手')
const userMsg = createUserMessage('你好')
const assistantMsg = createAssistantMessage('你好!有什么可以帮助你的吗?')响应状态检查
import { isClientError, isServerError, isSuccessResponse } from '@tcsk-ai/core'
const response = await client.chat(messages)
if (isSuccessResponse(response.code)) {
console.log('请求成功')
}
else if (isClientError(response.code)) {
console.log('客户端错误')
}
else if (isServerError(response.code)) {
console.log('服务端错误')
}任务ID生成
import { generateTaskId } from '@tcsk-ai/core'
const taskId = generateTaskId() // 生成唯一的任务ID错误处理
import { TCSKError } from '@tcsk-ai/core'
try {
const response = await client.chat(messages)
}
catch (error) {
if (error instanceof TCSKError) {
console.log('错误代码:', error.code)
console.log('HTTP状态码:', error.statusCode)
console.log('是否是认证错误:', error.isAuthError)
console.log('是否是网络错误:', error.isNetworkError)
}
}注意事项
Token管理: 客户端会自动管理访问令牌的获取和刷新,无需手动处理。
错误重试: 当遇到token过期错误时,客户端会自动重新获取token并重试请求。
流式响应: 流式聊天支持实时接收AI生成的内容,适合长文本生成场景。
配置验证: 使用
validateConfig函数可以在创建客户端前验证配置的完整性。
License
MIT
