2025-07-09 19:58:44 +08:00
|
|
|
|
import logging
|
|
|
|
|
from typing import Tuple
|
2025-07-02 17:49:02 +08:00
|
|
|
|
import requests
|
|
|
|
|
from ollama import Client, ResponseError
|
|
|
|
|
import tiktoken
|
2025-07-09 19:58:44 +08:00
|
|
|
|
import random
|
|
|
|
|
from Utils.AIGCLog import AIGCLog
|
2025-07-02 17:49:02 +08:00
|
|
|
|
|
|
|
|
|
class AICore:
|
|
|
|
|
modelMaxTokens = 128000
|
|
|
|
|
# 初始化 DeepSeek 使用的 Tokenizer (cl100k_base)
|
|
|
|
|
encoder = tiktoken.get_encoding("cl100k_base")
|
2025-07-09 19:58:44 +08:00
|
|
|
|
logger = AIGCLog(name = "AIGC", log_file = "aigc.log")
|
2025-07-02 17:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, model):
|
|
|
|
|
#初始化ollama客户端
|
2025-07-03 19:01:27 +08:00
|
|
|
|
self.ollamaClient = Client(host='http://localhost:11434', headers={'x-some-header': 'some-value'})
|
2025-07-09 19:58:44 +08:00
|
|
|
|
self.modelName = model
|
2025-07-03 19:01:27 +08:00
|
|
|
|
response = self.ollamaClient.show(model)
|
2025-07-02 17:49:02 +08:00
|
|
|
|
modelMaxTokens = response.modelinfo['qwen2.context_length']
|
|
|
|
|
|
|
|
|
|
def getPromptToken(self, prompt)-> int:
|
|
|
|
|
tokens = self.encoder.encode(prompt)
|
|
|
|
|
return len(tokens)
|
2025-07-09 19:58:44 +08:00
|
|
|
|
|
|
|
|
|
def generateAI(self, promptStr: str) -> Tuple[bool, str]:
|
|
|
|
|
try:
|
|
|
|
|
response = self.ollamaClient.generate(
|
|
|
|
|
model = self.modelName,
|
|
|
|
|
stream = False,
|
|
|
|
|
prompt = promptStr,
|
|
|
|
|
options={
|
|
|
|
|
"temperature": random.uniform(1.0, 1.5),
|
|
|
|
|
"repeat_penalty": 1.2, # 抑制重复
|
|
|
|
|
"top_p": random.uniform(0.7, 0.95),
|
|
|
|
|
"num_ctx": 4096, # 上下文长度
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return True, response.response
|
|
|
|
|
except ResponseError as e:
|
|
|
|
|
if e.status_code == 503:
|
|
|
|
|
print("🔄 服务不可用,5秒后重试...")
|
|
|
|
|
return False,"ollama 服务不可用"
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"🔥 未预料错误: {str(e)}")
|
|
|
|
|
return False, "未预料错误"
|