컴퓨터 채팅 고침: 부르면 매번 빨리 대답(느리거나 모델 없으면 내장 대답), 혼잣말은 판에서 눈에 띈 일에만 드물게, 수와 어긋난 말("풀 베팅인데 쫄았네") 거르고 수 종류별 한마디
- 대답: 이름·이어 말하기·대화 중인 컴퓨터에 매번 대답, 입력 중에 온 말은 끝나고 대답, 모델 2.5초만 기다리고 내장 대답, 한 사람에게 1분 8번까지 - 모델 대기열 1칸(GPU 하나), 대답이 혼잣말보다 먼저, 혼잣말은 바쁘면 버림, 타자 0.7~3.5초 - 혼잣말: 센 베팅·다이·고/스톱 등 눈에 띈 수에만, 방 40초·컴퓨터 90초 간격, 시작 인사·끼어들기 없앰, 수의 뜻과 판 공개 정보(판돈·칩·점수)를 모델에 줌 - 수 종류(센 베팅·콜·체크/삥·다이/폴드·과감·안전·기권)별 내장 한마디, 어긋나거나 게임과 상관없는 말은 그걸로 바꿈 - 막말·지시문 되풀이 거름, 마피아·라이어 게임은 계획된 대사만 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,7 +13,10 @@ export interface LlmConfig {
|
||||
/** Ollama base URL, e.g. http://192.168.10.9:11434. */
|
||||
url: string;
|
||||
model: string;
|
||||
/** Requests running at once (one GPU). */
|
||||
/**
|
||||
* Requests sent at once. Default 1: Ollama on the one GPU runs one request at a time (`n_slots = 1`), so a second
|
||||
* one would only wait inside Ollama — where an answer to a person could not get ahead of small talk.
|
||||
*/
|
||||
maxConcurrent?: number;
|
||||
/** Requests allowed to wait; more are refused at once (the caller uses a built-in line). */
|
||||
maxQueue?: number;
|
||||
@@ -39,7 +42,7 @@ export interface LlmStats {
|
||||
|
||||
export class Llm {
|
||||
private running = 0;
|
||||
private waiting: (() => void)[] = [];
|
||||
private waiting: { go: () => void; high: boolean }[] = [];
|
||||
private totalMs = 0;
|
||||
private backoffUntil = 0;
|
||||
private failures = 0;
|
||||
@@ -60,17 +63,31 @@ export class Llm {
|
||||
|
||||
/**
|
||||
* One chat completion. `format` is a JSON schema for structured answers. Returns the text, or null when it could
|
||||
* not be had in time (busy, down, timeout).
|
||||
* not be had in time (busy, down, timeout). `priority`: `high` (answering a person) goes ahead of everything that
|
||||
* waits; `low` (small talk nobody asked for) is dropped at once when the model is busy, so it never delays the rest.
|
||||
* `stop` ends the answer early (one chat line: a newline).
|
||||
*/
|
||||
async chat(messages: LlmMessage[], opts: { maxTokens?: number; temperature?: number; format?: object; timeoutMs?: number } = {}): Promise<string | null> {
|
||||
async chat(
|
||||
messages: LlmMessage[],
|
||||
opts: { maxTokens?: number; temperature?: number; format?: object; timeoutMs?: number; priority?: 'high' | 'normal' | 'low'; stop?: string[] } = {},
|
||||
): Promise<string | null> {
|
||||
this.stats.requests++;
|
||||
const full = this.running >= (this.cfg.maxConcurrent ?? 2) && this.waiting.length >= (this.cfg.maxQueue ?? 6);
|
||||
if (!this.available() || full) {
|
||||
const max = this.cfg.maxConcurrent ?? 1;
|
||||
const busy = this.running >= max;
|
||||
const full = busy && this.waiting.length >= (this.cfg.maxQueue ?? 6) && opts.priority !== 'high';
|
||||
if (!this.available() || full || (busy && opts.priority === 'low')) {
|
||||
this.stats.skipped++;
|
||||
return null;
|
||||
}
|
||||
if (this.running >= (this.cfg.maxConcurrent ?? 2)) await new Promise<void>((r) => this.waiting.push(r));
|
||||
this.running++;
|
||||
if (busy) {
|
||||
await new Promise<void>((go) => {
|
||||
const high = opts.priority === 'high';
|
||||
// Answers wait only behind earlier answers.
|
||||
const at = high ? this.waiting.findIndex((w) => !w.high) : -1;
|
||||
if (at >= 0) this.waiting.splice(at, 0, { go, high });
|
||||
else this.waiting.push({ go, high });
|
||||
});
|
||||
} else this.running++; // a waiter is handed the finished request's place (`running` stays)
|
||||
const t0 = this.now();
|
||||
const ctl = new AbortController();
|
||||
const timer = setTimeout(() => ctl.abort(), opts.timeoutMs ?? this.cfg.timeoutMs ?? 6000);
|
||||
@@ -85,7 +102,7 @@ export class Llm {
|
||||
stream: false,
|
||||
keep_alive: this.cfg.keepAlive ?? '2h',
|
||||
...(opts.format ? { format: opts.format } : {}),
|
||||
options: { temperature: opts.temperature ?? 0.9, num_predict: opts.maxTokens ?? 80 },
|
||||
options: { temperature: opts.temperature ?? 0.9, num_predict: opts.maxTokens ?? 80, ...(opts.stop ? { stop: opts.stop } : {}) },
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
@@ -110,8 +127,9 @@ export class Llm {
|
||||
return null;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
this.running--;
|
||||
this.waiting.shift()?.();
|
||||
const next = this.waiting.shift();
|
||||
if (next) next.go();
|
||||
else this.running--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user