Python 使用OpenAi库 输出流式数据

Python 使用OpenAi库 输出流式数据

import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import os
import base64
import json
import uuid
import requests
import uvicorn
from openai import OpenAI
from fastapi import FastAPI,HTTPException
from fastapi.responses import FileResponse,Response,StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

import speech_recognition as sr

class VoiceRecognizer:
    def __init__(self):
        self.recognizer = sr.Recognizer()
        self.microphone = sr.Microphone()
        
        # 调整麦克风噪声
        with self.microphone as source:
            self.recognizer.adjust_for_ambient_noise(source)
    
    def recognize_from_mic(self):
        with self.microphone as source:
            print("正在监听...")
            # 设置超时和短语时间
            audio = self.recognizer.listen(
                source,
                timeout=5,
                phrase_time_limit=5
            )
            return self.process_audio(audio)
    
    def recognize_from_file(self, audio_file):
        with sr.AudioFile(audio_file) as source:
            audio = self.recognizer.record(source)
            return self.process_audio(audio)
    
    def process_audio(self, audio):
        try:
            # 使用Google语音识别
            text = self.recognizer.recognize_google(
                audio,
                language='zh-CN',
                show_all=True
            )
            return text
        except sr.UnknownValueError:
            return "无法识别语音"
        except sr.RequestError:
            return "服务请求失败"


def read_json(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        config = json.load(file)
    return config

config = read_json('config.json')
print(config)

client = OpenAI(api_key=config['al']['api_key'], base_url=config['al']['base_url'])

async def process_openai_stream(response):
    """处理 OpenAI 流式响应"""
    for chunk in response:
        if chunk.choices[0].delta.content is not None:
            yield chunk.choices[0].delta.content

@app.post("/chat")
async def chat(request:dict):
    if "messages" not in request:
        raise HTTPException(status_code=400, detail="messages is required")
    messages = request["messages"]
    try:
        response = client.chat.completions.create(
            model="deepseek-r1-distill-llama-70b",
            messages=messages,
            stream=True,
        )
        print(response)
        # 将数据封装成 JSON 并返回
        return StreamingResponse(
            content=process_openai_stream(response),
            media_type="text/event-stream"
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/text_to_speech")   
def text_to_speech(request:dict):
    if "message" not in request:
        raise HTTPException(status_code=400, detail="message is required")
    message = request["message"]
    try:
        header = {"Authorization": f"Bearer;{config['hs']['access_token']}"}
        request_json = {
            "app": {
                "appid": config['hs']['appid'],
                "token": "access_token",
                "cluster": config['hs']['cluster']
            },
            "user": {
                "uid": "388808087185088"
            },
            "audio": {
                "voice_type": config['hs']['voice_type'],
                "emotion": config['hs']['emotion'],
                "language": config['hs']['language'],
                "encoding": "mp3",
                "speed_ratio": 1.0,
                "volume_ratio": 1.0,
                "pitch_ratio": 1.0,
            },
            "request": {
                "reqid": str(uuid.uuid4()),
                "text": message,
                "text_type": "plain",
                "operation": "query",
                "with_frontend": 1,
                "frontend_type": "unitTson"

            }
        }
        resp = requests.post(config['hs']['api_url'], json.dumps(request_json), headers=header)
        return Response(content=json.dumps(resp.json()), media_type="application/json")
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))





# import torch
# from TTS.api import TTS
# # Get device
# device = "cuda" if torch.cuda.is_available() else "cpu"

# # List available 🐸TTS models
# # print(TTS().list_models()) 

# # Initialize TTS
# tts_model = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)

# # List speakers
# # print(tts_model.speakers)

# @app.post("/text_to_speech1")   
# def api(request:dict):
#     print(request)
#     if "message" not in request:
#         raise HTTPException(status_code=400, detail="message is required")
#     message = request["message"]
#     try:
#         # 创建一个字节流对象
#         audio_bytes_io = io.BytesIO()
#         # 生成音频,这里先保存到临时文件
#         temp_file_path = "temp_audio.wav"
#         tts_model.tts_to_file(text=message, file_path=temp_file_path,speaker="Craig Gutsy", language="zh")
#         # 检查临时文件是否生成成功
#         if not os.path.exists(temp_file_path):
#             raise HTTPException(status_code=500, detail="生成TTS失败,没有创建临时文件!")
#         # 将临时文件内容写入字节流  
#         with open(temp_file_path, "rb") as f:
#             audio_bytes_io.write(f.read())
#         # 删除临时文件
#         os.remove(temp_file_path)
#         # 移动指针到字节流开头
#         audio_bytes_io.seek(0)
#         # 返回文件流响应
#         return StreamingResponse(audio_bytes_io, media_type="audio/wav")
#     except Exception as e:
#         raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    uvicorn.run(app, port=8086)


### OpenAI 流式输出的方法 为了实现OpenAI流式输出功能,开发者可以通过设置`stream=True`参数来启用这一特性。当此参数被激活时,API将以增量方返回数据片段而不是一次性返回整个响应[^3]。 对于Python环境中的具体应用而言,在使用`openai.ChatCompletion.create()`方法创建聊天完成请求的时候传递该选项即可开启流模。每当有新的消息部分可用时就会触发相应的事件处理程序,从而允许应用程序逐步接收并显示这些更新给最终用户[^2]。 下面是利用标准同步版本以及异步版(借助于`asyncio`)两种不同风格编写出来的简单例子: #### 同步流式输出实例 ```python import openai def sync_streaming_response(prompt, api_key): response = "" completion = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=150, n=1, stop=None, temperature=0.7, stream=True, # Enable streaming mode. api_key=api_key ) for event in completion: chunk = event['choices'][0].get('text', '') print(chunk, end="|", flush=True) # Print each part as it arrives with a separator. response += chunk return response ``` #### 异步流式输出实例 ```python from asyncio import run import openai async def async_streaming_response(prompt, api_key): response = "" async for event in await openai.ChatCompletion.acreate( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], max_tokens=150, n=1, stop=None, temperature=0.7, stream=True, # Enable streaming mode. api_key=api_key ): delta = event.get('choices', [{}])[0].get('delta', {}).get('content') if delta is not None: print(delta, end="|", flush=True) # Print each part as it arrives with a separator. response += delta return response if __name__ == "__main__": user_prompt = input("Enter your message:") my_api_key = "<your_open_ai_api_key_here>" result = run(async_streaming_response(user_prompt, my_api_key)) print("\nFinal Response:", result) ``` 上述两个代码段展示了如何分别采用同步和异步的方OpenAI API获取实时的数据流,并将其逐片打印出来以便观察过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值