返回 AiToEarn
dashscope.service.ts
根目录 / project / aitoearn-backend / apps / aitoearn-ai / src / core / ai / libs / dashscope / dashscope.service.ts
1 import { Injectable } from '@nestjs/common'
2 import axios, { AxiosInstance, AxiosResponse } from 'axios'
3 import { AiAvailabilityService } from '../../../ai-availability'
4 import { DashscopeConfig } from './dashscope.config'
5 import { DashscopeCreateVideoTaskRequest, DashscopeCreateVideoTaskResponse, DashscopeQueryVideoTaskResponse } from './dashscope.interface'
6
7 @Injectable()
8 export class DashscopeService {
9 private readonly httpClient: AxiosInstance
10
11 constructor(
12 private readonly config: DashscopeConfig,
13 private readonly aiAvailability: AiAvailabilityService,
14 ) {
15 this.httpClient = axios.create({
16 baseURL: this.config.baseUrl,
17 timeout: 60000,
18 headers: {
19 'Content-Type': 'application/json',
20 'Accept': 'application/json',
21 'Authorization': `Bearer ${this.config.apiKey}`,
22 },
23 })
24 }
25
26 async createVideoTask(request: DashscopeCreateVideoTaskRequest): Promise<DashscopeCreateVideoTaskResponse> {
27 return this.aiAvailability.execute(
28 { provider: 'dashscope', operation: 'createVideoTask', model: request.model },
29 async () => {
30 const response: AxiosResponse<DashscopeCreateVideoTaskResponse> = await this.httpClient.post(
31 '/api/v1/services/aigc/video-generation/video-synthesis',
32 request,
33 { headers: { 'X-DashScope-Async': 'enable' } },
34 )
35 return response.data
36 },
37 )
38 }
39
40 async getVideoTask(taskId: string): Promise<DashscopeQueryVideoTaskResponse> {
41 return this.aiAvailability.execute(
42 { provider: 'dashscope', operation: 'getVideoTask' },
43 async () => {
44 const response: AxiosResponse<DashscopeQueryVideoTaskResponse> = await this.httpClient.get(
45 `/api/v1/tasks/${encodeURIComponent(taskId)}`,
46 )
47 return response.data
48 },
49 )
50 }
51 }
52
52 lines TYPESCRIPT