feat: add NSFW script generation feature and Grok API configuration
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Any, Callable, Dict, Optional
|
||||
|
||||
import httpx
|
||||
@@ -8,6 +9,22 @@ import httpx
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def strip_grok_thinking(text: str) -> str:
|
||||
lines = text.split('\n')
|
||||
cleaned = []
|
||||
for line in lines:
|
||||
if line.startswith('> '):
|
||||
continue
|
||||
cleaned.append(line)
|
||||
result = []
|
||||
for line in cleaned:
|
||||
if result and line and not line.startswith('【') and result[-1] != '':
|
||||
result[-1] += line
|
||||
else:
|
||||
result.append(line)
|
||||
return '\n'.join(result).strip()
|
||||
|
||||
|
||||
class LLMService:
|
||||
def __init__(self, base_url: str, api_key: str, model: str):
|
||||
self.base_url = base_url.rstrip("/")
|
||||
@@ -68,11 +85,15 @@ class LLMService:
|
||||
if not raw:
|
||||
raise ValueError("LLM returned empty response")
|
||||
if raw.startswith("```"):
|
||||
lines = raw.split("\n")
|
||||
inner = lines[1:]
|
||||
if inner and inner[-1].strip().startswith("```"):
|
||||
inner = inner[:-1]
|
||||
raw = "\n".join(inner).strip()
|
||||
m = re.search(r'^```[a-z]*\n?([\s\S]*?)```\s*$', raw)
|
||||
if m:
|
||||
raw = m.group(1).strip()
|
||||
else:
|
||||
lines = raw.split("\n")
|
||||
inner = lines[1:]
|
||||
if inner and inner[-1].strip().startswith("```"):
|
||||
inner = inner[:-1]
|
||||
raw = "\n".join(inner).strip()
|
||||
if not raw:
|
||||
raise ValueError("LLM returned empty JSON after stripping markdown")
|
||||
try:
|
||||
@@ -115,11 +136,15 @@ class LLMService:
|
||||
if not raw:
|
||||
raise ValueError("LLM returned empty response")
|
||||
if raw.startswith("```"):
|
||||
lines = raw.split("\n")
|
||||
inner = lines[1:]
|
||||
if inner and inner[-1].strip().startswith("```"):
|
||||
inner = inner[:-1]
|
||||
raw = "\n".join(inner).strip()
|
||||
m = re.search(r'^```[a-z]*\n?([\s\S]*?)```\s*$', raw)
|
||||
if m:
|
||||
raw = m.group(1).strip()
|
||||
else:
|
||||
lines = raw.split("\n")
|
||||
inner = lines[1:]
|
||||
if inner and inner[-1].strip().startswith("```"):
|
||||
inner = inner[:-1]
|
||||
raw = "\n".join(inner).strip()
|
||||
if not raw:
|
||||
raise ValueError("LLM returned empty JSON after stripping markdown")
|
||||
try:
|
||||
@@ -379,3 +404,13 @@ class LLMService:
|
||||
if isinstance(result, list):
|
||||
return result
|
||||
return []
|
||||
|
||||
|
||||
class GrokLLMService(LLMService):
|
||||
async def stream_chat(self, system_prompt: str, user_message: str, on_token=None, max_tokens: int = 8192, usage_callback=None) -> str:
|
||||
full_text = await super().stream_chat(system_prompt, user_message, on_token, max_tokens=max_tokens, usage_callback=usage_callback)
|
||||
return strip_grok_thinking(full_text)
|
||||
|
||||
async def chat(self, system_prompt: str, user_message: str, usage_callback=None) -> str:
|
||||
full_text = await super().chat(system_prompt, user_message, usage_callback=usage_callback)
|
||||
return strip_grok_thinking(full_text)
|
||||
|
||||
Reference in New Issue
Block a user