亚洲中文字幕无码日韩精品,亚洲一区制服无码中字,亚洲精品第一国产综合精品99 ,一本大道中文日本香蕉

微立頂科技

新聞資訊

創(chuàng)新 服務 價值

  語音實時交互數字人,采用ollama的本地Qwen3:4B做LLM的代碼

發(fā)布日期:2025/5/7 7:40:59      瀏覽量:

語音實時交互數字人,采用ollama的本地Qwen3:4B做LLM代碼參考,以及設置人設Prompt


import time
import os
import requests
import json
from basereal import BaseReal
from logger import logger

def llm_response(message, nerfreal: BaseReal):
    start = time.perf_counter()
    
    # 構造包含系統(tǒng)提示和用戶消息的完整prompt
    full_prompt = f"""你是一個樂于助人的助手。請用中文回答用戶的問題。
    
用戶: {message}
助手:"""
    
    # Prepare the request data for Ollama API
    request_data = {
        "model": "qwen:4b",
        "prompt": full_prompt,  # 使用包含系統(tǒng)提示的完整prompt
        "stream": True,
        "options": {
            "temperature": 0.7,
            "top_p": 0.9
        }
    }
    
    end = time.perf_counter()
    logger.info(f"llm Time init: {end-start}s")

    # Make the request to local Ollama API
    response = requests.post(
        "http://localhost:11434/api/generate",
        json=request_data,
        stream=True
    )
    
    result = ""
    first = True
    
    for line in response.iter_lines():
        if line:
            # Decode the line and parse the JSON
            decoded_line = line.decode("utf-8")
            try:
                chunk = json.loads(decoded_line)
                if "response" in chunk:
                    msg = chunk["response"]
                    
                    if first:
                        end = time.perf_counter()
                        logger.info(f"llm Time to first chunk: {end-start}s")
                        first = False
                    
                    lastpos = 0
                    for i, char in enumerate(msg):
                        if char in ",.!;:,。???:;":
                            result = result + msg[lastpos:i+1]
                            lastpos = i+1
                            if len(result) > 10:
                                logger.info(result)
                                nerfreal.put_msg_txt(result)
                                result = ""
                    result = result + msg[lastpos:]
                    
            except json.JSONDecodeError:
                logger.error(f"Failed to parse JSON: {decoded_line}")
    
    end = time.perf_counter()
    logger.info(f"llm Time to last chunk: {end-start}s")
    if result:  # Send any remaining text
        nerfreal.put_msg_txt(result)





  業(yè)務實施流程

需求調研 →

團隊組建和動員 →

數據初始化 →

調試完善 →

解決方案和選型 →

硬件網絡部署 →

系統(tǒng)部署試運行 →

系統(tǒng)正式上線 →

合作協(xié)議

系統(tǒng)開發(fā)/整合

制作文檔和員工培訓

售后服務

馬上咨詢: 如果您有業(yè)務方面的問題或者需求,歡迎您咨詢!我們帶來的不僅僅是技術,還有行業(yè)經驗積累。
QQ: 39764417/308460098     Phone: 13 9800 1 9844 / 135 6887 9550     聯(lián)系人:石先生/雷先生