3 GIL 管理与并行执行 sequenceDiagram participant MT as 主线程 participant T1 as 工作线程1 participant T2 as 工作线程2 participant T3 as 工作线程3 MT->>T1: 启动 (nogil 区域) MT->>T2: 启动 (nogil 区域) MT->>T3: 启动 (nogil 区域) Note over T1,T3: GIL 已释放,真正并行 T1->>T1: C 级别计算 T2->>T2: C 级别计算 T3->>T3: C 级别计算 T1-->>MT: 完成 (获取 GIL) T2-->>MT: 完成 (获取 GIL) T3-->>MT: 完成 (获取 GIL) 三、Cython 优化的生产级代码实践 以下代码展示一个典型的 ML 数据预处理场景——Softmax 计算,对比纯 Python、NumPy 和 Cython 三种实现的性能差异。
前言:奥运男足为什么看不了直播了奥运男足直播不了这点很好解释。因为现在中国男足的水平还不高。
import OpenAI from 'openai'import type { LlmConfig, ChatMessage } from '../types'import { LLM_CONFIG } from '../constants'class LlmService { private openai: OpenAI | null = null private currentApiKey: string = '' private initClient(config: LlmConfig): void { if (this.currentApiKey === config.apiKey && this.openai) { return // 避免重复初始化 } const baseURL = config.baseURL || LLM_CONFIG.BASE_URL this.openai = new OpenAI({ apiKey: config.apiKey, dangerouslyAllowBrowser: true, // 允许浏览器端调用 baseURL: baseURL, // 确保使用 fetch API 支持流式 fetch: (url, init) => { console.log('LLM请求URL:', url) console.log('LLM请求配置:', { method: init?.method, headers: init?.headers, body: init?.body }) return fetch(url, init) } }) this.currentApiKey = config.apiKey } // 普通对话模式 async sendMessage(config: LlmConfig, userMessage: string): Promise { this.initClient(config) if (!this.openai) { throw new Error('LLM客户端未初始化') } const messages: ChatMessage[] = [ { role: 'system', content: LLM_CONFIG.SYSTEM_PROMPT }, { role: 'user', content: userMessage } ] try { const completion = await this.openai.chat.completions.create({ messages, model: config.model }) const response = completion.choices[0]?.message?.content return response || null } catch (error) { console.error('LLM请求失败:', error) throw error } } // 流式对话模式(核心) async sendMessageWithStream(config: LlmConfig, userMessage: string): Promise { this.initClient(config) if (!this.openai) { throw new Error('LLM客户端未初始化') } const messages: ChatMessage[] = [ { role: 'system', content: LLM_CONFIG.SYSTEM_PROMPT }, { role: 'user', content: userMessage } ] try { const stream = await this.openai.chat.completions.create({ messages, model: config.model, stream: true // 开启流式输出 }) // 返回异步迭代器,逐字输出 return (async function* () { let chunkCount = 0 for await (const part of stream) { chunkCount++ const content = part.choices[0]?.delta?.content if (content) { yield content } } })() } catch (error) { console.error('流式请求失败:', error) throw error } }}export const llmService = new LlmService()设计要点:dangerouslyAllowBrowser: true:允许在浏览器端直接调用 LLM API,适用于 Demo 场景自定义 fetch:拦截请求用于日志追踪,便于调试流式数据sendMessageWithStream 返回 AsyncIterable:调用方可通过 for await 逐字消费,实现流式播报API Key 缓存:currentApiKey 避免重复初始化 OpenAI 客户端4.5 action-manager.ts - 流式播报动作队列流式播报是灵引的核心交互机制。当 LLM 流式返回文本时,需要将文本分段发送给数字人 SDK 进行语音合成和动作驱动。ActionManager 通过队列模式管理这些播报段落,确保按顺序播放。
作为一款中式探险题材游戏,《超自然行动组》2025年11月曾邀请沿河土家族自治县傩面具雕刻第七代传人杨云霞,共同打造傩戏联动时装,让非遗技艺以数字化形式走进年轻群体。