整理ollma接口,添加token个数限制接口
This commit is contained in:
parent
260b014a84
commit
5705ab962a
21
AIGC/AICore.py
Normal file
21
AIGC/AICore.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import requests
|
||||||
|
from ollama import Client, ResponseError
|
||||||
|
import tiktoken
|
||||||
|
|
||||||
|
class AICore:
|
||||||
|
modelMaxTokens = 128000
|
||||||
|
# 初始化 DeepSeek 使用的 Tokenizer (cl100k_base)
|
||||||
|
encoder = tiktoken.get_encoding("cl100k_base")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, model):
|
||||||
|
#初始化ollama客户端
|
||||||
|
ollamaClient = Client(host='http://localhost:11434', headers={'x-some-header': 'some-value'})
|
||||||
|
response = ollamaClient.show(model)
|
||||||
|
modelMaxTokens = response.modelinfo['qwen2.context_length']
|
||||||
|
|
||||||
|
def getPromptToken(self, prompt)-> int:
|
||||||
|
tokens = self.encoder.encode(prompt)
|
||||||
|
return len(tokens)
|
||||||
|
|
@ -11,8 +11,9 @@ from fastapi import FastAPI, Request, HTTPException, WebSocket, WebSocketDisconn
|
|||||||
from fastapi.websockets import WebSocketState
|
from fastapi.websockets import WebSocketState
|
||||||
from h11 import ConnectionClosed
|
from h11 import ConnectionClosed
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
from AICore import AICore
|
||||||
from Utils.AIGCLog import AIGCLog
|
from Utils.AIGCLog import AIGCLog
|
||||||
from ollama import Client, ResponseError
|
|
||||||
|
|
||||||
app = FastAPI(title = "AI 通信服务")
|
app = FastAPI(title = "AI 通信服务")
|
||||||
logger = AIGCLog(name = "AIGC", log_file = "aigc.log")
|
logger = AIGCLog(name = "AIGC", log_file = "aigc.log")
|
||||||
@ -27,8 +28,8 @@ logger.log(logging.INFO, f"使用的模型是 {args.model}")
|
|||||||
maxAIRegerateCount = 5
|
maxAIRegerateCount = 5
|
||||||
lastPrompt = ""
|
lastPrompt = ""
|
||||||
|
|
||||||
#初始化ollama客户端
|
aicore = AICore(args.model)
|
||||||
ollamaClient = Client(host='http://localhost:11434')
|
|
||||||
|
|
||||||
async def heartbeat(websocket: WebSocket):
|
async def heartbeat(websocket: WebSocket):
|
||||||
pass
|
pass
|
||||||
@ -150,6 +151,7 @@ async def generateAIChat(promptStr: str, websocket: WebSocket| None = None):
|
|||||||
{"role": "system", "content": promptStr}
|
{"role": "system", "content": promptStr}
|
||||||
]
|
]
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# response = ollamaClient.chat(
|
# response = ollamaClient.chat(
|
||||||
# model = args.model,
|
# model = args.model,
|
||||||
# stream = False,
|
# stream = False,
|
||||||
@ -235,6 +237,7 @@ if __name__ == "__main__":
|
|||||||
server_thread.start()
|
server_thread.start()
|
||||||
|
|
||||||
# Test
|
# Test
|
||||||
|
aicore.getPromptToken("测试功能")
|
||||||
asyncio.run(
|
asyncio.run(
|
||||||
generateAIChat(promptStr = f"""
|
generateAIChat(promptStr = f"""
|
||||||
#你是一个游戏NPC对话生成器。请严格按以下要求生成两个角色的日常对话
|
#你是一个游戏NPC对话生成器。请严格按以下要求生成两个角色的日常对话
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
uvicorn[standard]
|
uvicorn[standard]
|
||||||
fastapi
|
fastapi
|
||||||
ollama
|
ollama
|
||||||
|
tiktoken
|
Loading…
x
Reference in New Issue
Block a user