2024-02-24 12:28:55 +01:00
|
|
|
import asyncio
|
2024-02-24 19:16:04 +01:00
|
|
|
import collections
|
2024-02-24 12:28:55 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import subprocess
|
2024-03-17 19:12:37 +01:00
|
|
|
import sys
|
|
|
|
import threading
|
2024-02-24 12:28:55 +01:00
|
|
|
import time
|
|
|
|
from contextlib import closing
|
|
|
|
from re import RegexFlag
|
|
|
|
|
|
|
|
import aiohttp
|
2024-03-07 10:41:53 +01:00
|
|
|
import numpy as np
|
2024-02-24 12:28:55 +01:00
|
|
|
import openai
|
|
|
|
from behave import step
|
|
|
|
from behave.api.async_step import async_run_until_complete
|
2024-02-25 13:49:43 +01:00
|
|
|
from prometheus_client import parser
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step("a server listening on {server_fqdn}:{server_port}")
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_server_config(context, server_fqdn, server_port):
|
|
|
|
context.server_fqdn = server_fqdn
|
|
|
|
context.server_port = int(server_port)
|
2024-03-20 13:14:32 +01:00
|
|
|
context.n_gpu_layer = None
|
2024-02-24 12:28:55 +01:00
|
|
|
if 'PORT' in os.environ:
|
|
|
|
context.server_port = int(os.environ['PORT'])
|
|
|
|
print(f"$PORT set, overriding server port with to {context.server_port}")
|
2024-03-07 10:41:53 +01:00
|
|
|
if 'FQDN' in os.environ:
|
|
|
|
context.server_fqdn = os.environ['FQDN']
|
|
|
|
print(f"$FQDN set, overriding server fqdn with to {context.server_fqdn}")
|
2024-03-20 13:14:32 +01:00
|
|
|
if 'N_GPU_LAYERS' in os.environ:
|
|
|
|
context.n_gpu_layer = int(os.environ['N_GPU_LAYERS'])
|
|
|
|
print(f"$N_GPU_LAYERS set, overriding n_gpu_layer with to {context.n_gpu_layer}")
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
context.base_url = f'http://{context.server_fqdn}:{context.server_port}'
|
|
|
|
|
|
|
|
context.model_alias = None
|
2024-03-17 19:12:37 +01:00
|
|
|
context.model_file = None
|
2024-03-23 18:07:00 +01:00
|
|
|
context.model_hf_repo = None
|
|
|
|
context.model_hf_file = None
|
2024-03-17 19:12:37 +01:00
|
|
|
context.model_url = None
|
2024-03-02 22:00:14 +01:00
|
|
|
context.n_batch = None
|
2024-03-13 18:54:21 +01:00
|
|
|
context.n_ubatch = None
|
2024-02-24 12:28:55 +01:00
|
|
|
context.n_ctx = None
|
2024-03-02 22:00:14 +01:00
|
|
|
context.n_ga = None
|
|
|
|
context.n_ga_w = None
|
2024-02-24 12:28:55 +01:00
|
|
|
context.n_predict = None
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = 0
|
2024-02-24 12:28:55 +01:00
|
|
|
context.n_server_predict = None
|
2024-04-08 14:43:30 +02:00
|
|
|
context.slot_save_path = None
|
|
|
|
context.id_slot = None
|
|
|
|
context.cache_prompt = None
|
2024-02-24 12:28:55 +01:00
|
|
|
context.n_slots = None
|
2024-03-02 22:00:14 +01:00
|
|
|
context.prompt_prefix = None
|
|
|
|
context.prompt_suffix = None
|
2024-02-24 12:28:55 +01:00
|
|
|
context.server_api_key = None
|
|
|
|
context.server_continuous_batching = False
|
|
|
|
context.server_embeddings = False
|
2024-02-25 13:49:43 +01:00
|
|
|
context.server_metrics = False
|
|
|
|
context.server_process = None
|
2024-03-02 22:00:14 +01:00
|
|
|
context.seed = None
|
2024-04-24 11:08:36 +02:00
|
|
|
context.draft = None
|
2024-02-24 12:28:55 +01:00
|
|
|
context.server_seed = None
|
|
|
|
context.user_api_key = None
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
context.response_format = None
|
2024-05-01 17:52:55 +02:00
|
|
|
context.temperature = None
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
context.tasks_result = []
|
|
|
|
context.concurrent_tasks = []
|
|
|
|
context.prompts = []
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a model file {hf_file} from HF repo {hf_repo}')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_download_hf_model(context, hf_file, hf_repo):
|
2024-03-23 18:07:00 +01:00
|
|
|
context.model_hf_repo = hf_repo
|
|
|
|
context.model_hf_file = hf_file
|
|
|
|
context.model_file = os.path.basename(hf_file)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-17 19:12:37 +01:00
|
|
|
@step('a model file {model_file}')
|
|
|
|
def step_model_file(context, model_file):
|
|
|
|
context.model_file = model_file
|
|
|
|
|
|
|
|
|
|
|
|
@step('a model url {model_url}')
|
|
|
|
def step_model_url(context, model_url):
|
|
|
|
context.model_url = model_url
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a model alias {model_alias}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_model_alias(context, model_alias):
|
|
|
|
context.model_alias = model_alias
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{seed:d} as server seed')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_seed(context, seed):
|
2024-03-02 22:00:14 +01:00
|
|
|
context.server_seed = seed
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{ngl:d} GPU offloaded layers')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_n_gpu_layer(context, ngl):
|
|
|
|
if 'N_GPU_LAYERS' in os.environ:
|
|
|
|
new_ngl = int(os.environ['N_GPU_LAYERS'])
|
|
|
|
if context.debug:
|
|
|
|
print(f"-ngl upgraded from {ngl} to {new_ngl}")
|
|
|
|
ngl = new_ngl
|
|
|
|
context.n_gpu_layer = ngl
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-04-24 11:08:36 +02:00
|
|
|
@step('{draft:d} as draft')
|
|
|
|
def step_draft(context, draft):
|
|
|
|
context.draft = draft
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_ctx:d} KV cache size')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_n_ctx(context, n_ctx):
|
2024-03-02 22:00:14 +01:00
|
|
|
context.n_ctx = n_ctx
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_slots:d} slots')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_n_slots(context, n_slots):
|
2024-03-02 22:00:14 +01:00
|
|
|
context.n_slots = n_slots
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_predict:d} server max tokens to predict')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_server_n_predict(context, n_predict):
|
2024-03-02 22:00:14 +01:00
|
|
|
context.n_server_predict = n_predict
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-04-08 14:43:30 +02:00
|
|
|
@step('{slot_save_path} as slot save path')
|
|
|
|
def step_slot_save_path(context, slot_save_path):
|
|
|
|
context.slot_save_path = slot_save_path
|
|
|
|
|
|
|
|
|
|
|
|
@step('using slot id {id_slot:d}')
|
|
|
|
def step_id_slot(context, id_slot):
|
|
|
|
context.id_slot = id_slot
|
|
|
|
|
|
|
|
|
|
|
|
@step('prompt caching is enabled')
|
|
|
|
def step_enable_prompt_cache(context):
|
|
|
|
context.cache_prompt = True
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('continuous batching')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_server_continuous_batching(context):
|
|
|
|
context.server_continuous_batching = True
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('embeddings extraction')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_server_embeddings(context):
|
|
|
|
context.server_embeddings = True
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('prometheus compatible metrics exposed')
|
2024-02-25 13:49:43 +01:00
|
|
|
def step_server_metrics(context):
|
|
|
|
context.server_metrics = True
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step("the server is starting")
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_start_server(context):
|
|
|
|
start_server_background(context)
|
|
|
|
attempts = 0
|
2024-03-14 12:15:39 +01:00
|
|
|
max_attempts = 20
|
|
|
|
if 'GITHUB_ACTIONS' in os.environ:
|
|
|
|
max_attempts *= 2
|
|
|
|
|
2024-03-20 06:33:49 +01:00
|
|
|
addrs = socket.getaddrinfo(context.server_fqdn, context.server_port, type=socket.SOCK_STREAM)
|
|
|
|
family, typ, proto, _, sockaddr = addrs[0]
|
|
|
|
|
2024-02-24 12:28:55 +01:00
|
|
|
while True:
|
2024-03-20 06:33:49 +01:00
|
|
|
with closing(socket.socket(family, typ, proto)) as sock:
|
|
|
|
result = sock.connect_ex(sockaddr)
|
2024-02-24 12:28:55 +01:00
|
|
|
if result == 0:
|
|
|
|
print("\x1b[33;46mserver started!\x1b[0m")
|
|
|
|
return
|
|
|
|
attempts += 1
|
2024-03-14 12:15:39 +01:00
|
|
|
if attempts > max_attempts:
|
2024-02-24 12:28:55 +01:00
|
|
|
assert False, "server not started"
|
|
|
|
print(f"waiting for server to start, connect error code = {result}...")
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step("the server is {expecting_status}")
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_wait_for_the_server_to_be_started(context, expecting_status):
|
|
|
|
match expecting_status:
|
|
|
|
case 'healthy':
|
2024-03-17 19:12:37 +01:00
|
|
|
await wait_for_health_status(context, context.base_url, 200, 'ok',
|
|
|
|
timeout=30)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
case 'ready' | 'idle':
|
|
|
|
await wait_for_health_status(context, context.base_url, 200, 'ok',
|
2024-03-02 22:00:14 +01:00
|
|
|
timeout=10,
|
2024-02-24 12:28:55 +01:00
|
|
|
params={'fail_on_no_slot': 0, 'include_slots': 0},
|
|
|
|
slots_idle=context.n_slots,
|
|
|
|
slots_processing=0,
|
|
|
|
expected_slots=[{'id': slot_id, 'state': 0}
|
2024-03-02 22:00:14 +01:00
|
|
|
for slot_id in
|
|
|
|
range(context.n_slots if context.n_slots else 1)])
|
2024-02-24 12:28:55 +01:00
|
|
|
case 'busy':
|
|
|
|
await wait_for_health_status(context, context.base_url, 503,
|
|
|
|
'no slot available',
|
|
|
|
params={'fail_on_no_slot': 0, 'include_slots': 0},
|
|
|
|
slots_idle=0,
|
|
|
|
slots_processing=context.n_slots,
|
|
|
|
expected_slots=[{'id': slot_id, 'state': 1}
|
2024-03-02 22:00:14 +01:00
|
|
|
for slot_id in
|
|
|
|
range(context.n_slots if context.n_slots else 1)])
|
2024-02-24 12:28:55 +01:00
|
|
|
case _:
|
|
|
|
assert False, "unknown status"
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('all slots are {expected_slot_status_string}')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_all_slots_status(context, expected_slot_status_string):
|
|
|
|
match expected_slot_status_string:
|
|
|
|
case 'idle':
|
|
|
|
expected_slot_status = 0
|
|
|
|
case 'busy':
|
|
|
|
expected_slot_status = 1
|
|
|
|
case _:
|
|
|
|
assert False, "unknown status"
|
|
|
|
|
|
|
|
expected_slots = [{'id': slot_id, 'state': expected_slot_status}
|
|
|
|
for slot_id in range(context.n_slots)]
|
|
|
|
await request_slots_status(context, expected_slots)
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a completion request with {api_error} api error')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_request_completion(context, api_error):
|
|
|
|
expect_api_error = api_error == 'raised'
|
2024-05-01 17:52:55 +02:00
|
|
|
seeds = await completions_seed(context, num_seeds=1)
|
2024-02-24 12:28:55 +01:00
|
|
|
completion = await request_completion(context.prompts.pop(),
|
2024-05-01 17:52:55 +02:00
|
|
|
seeds[0] if seeds is not None else seeds,
|
2024-02-24 12:28:55 +01:00
|
|
|
context.base_url,
|
|
|
|
debug=context.debug,
|
|
|
|
n_predict=context.n_predict,
|
2024-04-08 14:43:30 +02:00
|
|
|
cache_prompt=context.cache_prompt,
|
|
|
|
id_slot=context.id_slot,
|
2024-02-24 12:28:55 +01:00
|
|
|
expect_api_error=expect_api_error,
|
2024-05-01 17:52:55 +02:00
|
|
|
user_api_key=context.user_api_key,
|
|
|
|
temperature=context.temperature)
|
2024-02-24 12:28:55 +01:00
|
|
|
context.tasks_result.append(completion)
|
|
|
|
if context.debug:
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"Completion response: {completion}")
|
2024-02-24 12:28:55 +01:00
|
|
|
if expect_api_error:
|
|
|
|
assert completion == 401, f"completion must be an 401 status code: {completion}"
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{predicted_n:d} tokens are predicted matching {re_content}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_n_tokens_predicted_with_content(context, predicted_n, re_content):
|
2024-03-09 10:30:04 +01:00
|
|
|
context.completion = context.tasks_result.pop()
|
|
|
|
assert_n_tokens_predicted(context.completion, predicted_n, re_content)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{predicted_n:d} tokens are predicted')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_n_tokens_predicted(context, predicted_n):
|
2024-03-09 10:30:04 +01:00
|
|
|
context.completion = context.tasks_result.pop()
|
|
|
|
assert_n_tokens_predicted(context.completion, predicted_n)
|
|
|
|
|
|
|
|
|
2024-04-24 11:08:36 +02:00
|
|
|
@step('all predictions are equal')
|
|
|
|
@async_run_until_complete
|
|
|
|
async def step_predictions_equal(context):
|
|
|
|
n_completions = await gather_tasks_results(context)
|
|
|
|
assert n_completions >= 2, "need at least 2 completions"
|
|
|
|
assert_all_predictions_equal(context.tasks_result)
|
|
|
|
context.tasks_result = []
|
|
|
|
|
|
|
|
|
2024-05-01 17:52:55 +02:00
|
|
|
@step('all predictions are different')
|
|
|
|
@async_run_until_complete
|
|
|
|
async def step_predictions_equal(context):
|
|
|
|
n_completions = await gather_tasks_results(context)
|
|
|
|
assert n_completions >= 2, "need at least 2 completions"
|
|
|
|
assert_all_predictions_different(context.tasks_result)
|
|
|
|
context.tasks_result = []
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('the completion is truncated')
|
2024-03-09 10:30:04 +01:00
|
|
|
def step_assert_completion_truncated(context):
|
|
|
|
step_assert_completion_truncated(context, '')
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('the completion is {truncated} truncated')
|
2024-03-09 10:30:04 +01:00
|
|
|
def step_assert_completion_truncated(context, truncated):
|
|
|
|
truncated = truncated != "not"
|
|
|
|
assert context.completion['truncated'] == truncated, f'{context.completion}'
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_prompt:d} prompt tokens are processed')
|
2024-03-09 10:30:04 +01:00
|
|
|
def step_impl(context, n_prompt):
|
|
|
|
assert n_prompt < 0 or n_prompt == context.completion['timings']['prompt_n'], f"n_prompt={context.completion['timings']['prompt_n']}"
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a user prompt {user_prompt}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_user_prompt(context, user_prompt):
|
|
|
|
context.prompts.append(user_prompt)
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = len(context.prompts)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a system prompt {system_prompt}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_system_prompt(context, system_prompt):
|
|
|
|
context.system_prompt = system_prompt
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a model {model}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_model(context, model):
|
|
|
|
context.model = model
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{max_tokens:d} max tokens to predict')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_max_tokens(context, max_tokens):
|
2024-03-02 22:00:14 +01:00
|
|
|
context.n_predict = max_tokens
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
@step('a response format {response_format}')
|
|
|
|
def step_response_format(context, response_format):
|
|
|
|
context.response_format = json.loads(response_format)
|
|
|
|
|
|
|
|
|
2024-05-01 17:52:55 +02:00
|
|
|
@step('{temperature:f} temperature')
|
|
|
|
def step_temperature(context, temperature):
|
|
|
|
context.temperature = temperature
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('streaming is {enable_streaming}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_streaming(context, enable_streaming):
|
|
|
|
context.enable_streaming = enable_streaming == 'enabled'
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a user api key {user_api_key}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_user_api_key(context, user_api_key):
|
|
|
|
context.user_api_key = user_api_key
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('no user api key')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_no_user_api_key(context):
|
|
|
|
context.user_api_key = None
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a user api key ')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_no_user_api_key_space(context):
|
|
|
|
context.user_api_key = None
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a server api key {server_api_key}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_server_api_key(context, server_api_key):
|
|
|
|
context.server_api_key = server_api_key
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_junk:d} as number of junk')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_n_junk(context, n_junk):
|
|
|
|
context.n_junk = n_junk
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_batch:d} as batch size')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_n_batch(context, n_batch):
|
|
|
|
context.n_batch = n_batch
|
|
|
|
|
|
|
|
|
2024-03-13 18:54:21 +01:00
|
|
|
@step('{n_ubatch:d} as ubatch size')
|
|
|
|
def step_n_ubatch(context, n_ubatch):
|
|
|
|
context.n_ubatch = n_ubatch
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{seed:d} as seed')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_seed(context, seed):
|
2024-05-01 17:52:55 +02:00
|
|
|
if context.seed is None:
|
|
|
|
context.seed = [seed]
|
|
|
|
else:
|
|
|
|
context.seed.append(seed)
|
2024-03-02 22:00:14 +01:00
|
|
|
|
|
|
|
|
2024-05-08 14:27:58 +02:00
|
|
|
@step('BOS token is {bos:d}')
|
|
|
|
def step_bos_token(context, bos):
|
|
|
|
context.bos = bos
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a prefix prompt')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_prompt_prefix(context):
|
2024-03-10 18:17:47 +01:00
|
|
|
context.prompt_prefix = context_text(context)
|
2024-03-02 22:00:14 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a junk suffix prompt')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_prompt_junk_suffix(context):
|
2024-03-10 18:17:47 +01:00
|
|
|
context.prompt_junk_suffix = context_text(context)
|
2024-03-02 22:00:14 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a suffix prompt')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_prompt_suffix(context):
|
2024-03-10 18:17:47 +01:00
|
|
|
context.prompt_suffix = context_text(context)
|
2024-03-02 22:00:14 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_ga:d} group attention factor'
|
|
|
|
' to extend context size through self-extend')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_impl(context, n_ga):
|
|
|
|
context.n_ga = n_ga
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_ga_w:d} group attention width to extend context size through self-extend')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_impl(context, n_ga_w):
|
|
|
|
context.n_ga_w = n_ga_w
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a passkey prompt template')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_prompt_passkey(context):
|
2024-03-10 18:17:47 +01:00
|
|
|
context.prompt_passkey = context_text(context)
|
2024-03-02 22:00:14 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_prompts:d} fixed prompts')
|
2024-03-07 10:41:53 +01:00
|
|
|
def step_fixed_prompts(context, n_prompts):
|
|
|
|
context.prompts.extend([str(0)*(context.n_batch if context.n_batch is not None else 512) for i in range(n_prompts)])
|
|
|
|
context.n_prompts = n_prompts
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a "{passkey}" passkey challenge prompt with the passkey inserted every {i_pos:d} junk')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_prompt_passkey(context, passkey, i_pos):
|
|
|
|
prompt = ""
|
|
|
|
for i in range(context.n_junk):
|
|
|
|
if i % context.n_junk == i_pos:
|
|
|
|
prompt += context.prompt_passkey # the passkey is already substituted
|
|
|
|
prompt += context.prompt_junk_suffix
|
|
|
|
if context.debug:
|
|
|
|
passkey_highlight = "\x1b[33m" + passkey + "\x1b[0m"
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"Passkey challenge:\n```{prompt.replace(passkey, passkey_highlight)}```")
|
2024-03-02 22:00:14 +01:00
|
|
|
context.prompts.append(context.prompt_prefix + prompt + context.prompt_suffix)
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = len(context.prompts)
|
2024-03-02 22:00:14 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('an OAI compatible chat completions request with {api_error} api error')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_oai_chat_completions(context, api_error):
|
|
|
|
if context.debug:
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"Submitting OAI compatible completions request...")
|
2024-02-24 12:28:55 +01:00
|
|
|
expect_api_error = api_error == 'raised'
|
2024-05-01 17:52:55 +02:00
|
|
|
seeds = await completions_seed(context, num_seeds=1),
|
2024-02-24 12:28:55 +01:00
|
|
|
completion = await oai_chat_completions(context.prompts.pop(),
|
2024-05-01 17:52:55 +02:00
|
|
|
seeds[0] if seeds is not None else seeds,
|
2024-02-24 12:28:55 +01:00
|
|
|
context.system_prompt,
|
|
|
|
context.base_url,
|
2024-02-28 09:39:15 +01:00
|
|
|
'/v1/chat',
|
2024-02-24 12:28:55 +01:00
|
|
|
False,
|
|
|
|
model=context.model if hasattr(context, 'model') else None,
|
|
|
|
|
|
|
|
n_predict=context.n_predict
|
|
|
|
if hasattr(context, 'n_predict') else None,
|
|
|
|
|
|
|
|
enable_streaming=context.enable_streaming
|
|
|
|
if hasattr(context, 'enable_streaming') else None,
|
|
|
|
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
response_format=context.response_format
|
|
|
|
if hasattr(context, 'response_format') else None,
|
|
|
|
|
2024-02-24 12:28:55 +01:00
|
|
|
user_api_key=context.user_api_key
|
|
|
|
if hasattr(context, 'user_api_key') else None,
|
|
|
|
|
|
|
|
expect_api_error=expect_api_error)
|
|
|
|
context.tasks_result.append(completion)
|
|
|
|
if context.debug:
|
|
|
|
print(f"Completion response: {completion}")
|
|
|
|
if expect_api_error:
|
|
|
|
assert completion == 401, f"completion must be an 401 status code: {completion}"
|
|
|
|
|
|
|
|
if context.debug:
|
|
|
|
print(f"Completion response: {completion}")
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a prompt')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_a_prompt(context):
|
2024-03-10 18:17:47 +01:00
|
|
|
context.prompts.append(context_text(context))
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = len(context.prompts)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('a prompt {prompt}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_a_prompt_prompt(context, prompt):
|
|
|
|
context.prompts.append(prompt)
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = len(context.prompts)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-05-01 17:52:55 +02:00
|
|
|
@step('{num_prompts:d} prompts {prompt} with seed {seed:d}')
|
|
|
|
def step_many_prompts(context, num_prompts, prompt, seed):
|
|
|
|
if context.seed is None:
|
|
|
|
context.seed = []
|
|
|
|
for _ in range(num_prompts):
|
|
|
|
context.seed.append(seed)
|
|
|
|
context.prompts.append(prompt)
|
|
|
|
context.n_prompts = len(context.prompts)
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('concurrent completion requests')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete()
|
|
|
|
async def step_concurrent_completion_requests(context):
|
2024-05-01 17:52:55 +02:00
|
|
|
await concurrent_requests(
|
|
|
|
context,
|
|
|
|
request_completion,
|
|
|
|
# prompt is inserted automatically
|
|
|
|
context.base_url,
|
|
|
|
debug=context.debug,
|
|
|
|
prompt_prefix=context.prompt_prefix,
|
|
|
|
prompt_suffix=context.prompt_suffix,
|
|
|
|
n_predict=context.n_predict if hasattr(context, 'n_predict') else None,
|
|
|
|
user_api_key=context.user_api_key if hasattr(context, 'user_api_key') else None,
|
|
|
|
temperature=context.temperature,
|
|
|
|
)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('concurrent OAI completions requests')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_oai_chat_completions(context):
|
2024-02-24 19:16:04 +01:00
|
|
|
await concurrent_requests(context, oai_chat_completions,
|
|
|
|
# user_prompt is inserted automatically
|
|
|
|
context.system_prompt,
|
|
|
|
context.base_url,
|
2024-02-28 09:39:15 +01:00
|
|
|
'/v1/chat/completions',
|
|
|
|
True, # async_client
|
|
|
|
model=context.model
|
|
|
|
if hasattr(context, 'model') else None,
|
|
|
|
n_predict=context.n_predict
|
|
|
|
if hasattr(context, 'n_predict') else None,
|
|
|
|
enable_streaming=context.enable_streaming
|
|
|
|
if hasattr(context, 'enable_streaming') else None,
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
response_format=context.response_format
|
|
|
|
if hasattr(context, 'response_format') else None,
|
2024-02-28 09:39:15 +01:00
|
|
|
user_api_key=context.user_api_key
|
|
|
|
if hasattr(context, 'user_api_key') else None)
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('concurrent OAI completions requests no v1')
|
2024-02-28 09:39:15 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_oai_chat_completions(context):
|
|
|
|
await concurrent_requests(context, oai_chat_completions,
|
|
|
|
# user_prompt is inserted automatically
|
|
|
|
context.system_prompt,
|
|
|
|
context.base_url,
|
|
|
|
'/chat/completions',
|
2024-02-24 19:16:04 +01:00
|
|
|
True, # async_client
|
|
|
|
model=context.model
|
|
|
|
if hasattr(context, 'model') else None,
|
|
|
|
n_predict=context.n_predict
|
|
|
|
if hasattr(context, 'n_predict') else None,
|
|
|
|
enable_streaming=context.enable_streaming
|
|
|
|
if hasattr(context, 'enable_streaming') else None,
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
response_format=context.response_format
|
|
|
|
if hasattr(context, 'response_format') else None,
|
2024-02-24 19:16:04 +01:00
|
|
|
user_api_key=context.user_api_key
|
|
|
|
if hasattr(context, 'user_api_key') else None)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('all prompts are predicted')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_all_prompts_are_predicted(context):
|
|
|
|
await all_prompts_are_predicted(context)
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('all prompts are predicted with {n_expected_predicted:d} tokens')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
2024-03-02 22:00:14 +01:00
|
|
|
async def step_all_prompts_are_predicted_with_n_tokens(context, n_expected_predicted):
|
|
|
|
await all_prompts_are_predicted(context, n_expected_predicted)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def all_prompts_are_predicted(context, expected_predicted_n=None):
|
|
|
|
n_completions = await gather_tasks_results(context)
|
|
|
|
assert n_completions > 0
|
|
|
|
for i in range(n_completions):
|
|
|
|
assert_n_tokens_predicted(context.tasks_result.pop(), expected_predicted_n=expected_predicted_n)
|
|
|
|
assert len(context.concurrent_tasks) == 0, f"{len(context.concurrent_tasks)} pending requests"
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('embeddings are computed for')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_compute_embedding(context):
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = 1
|
2024-05-01 17:52:55 +02:00
|
|
|
context.embeddings = await request_embedding(context_text(context), None, base_url=context.base_url)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('all embeddings are the same')
|
2024-03-07 10:41:53 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_all_embeddings_are_the_same(context):
|
|
|
|
n_embedding_requests = await gather_tasks_results(context)
|
|
|
|
assert n_embedding_requests > 0
|
|
|
|
embeddings = []
|
|
|
|
for i in range(n_embedding_requests):
|
|
|
|
embedding = context.tasks_result.pop().pop()
|
|
|
|
embeddings.append(embedding)
|
|
|
|
assert_embeddings(embedding)
|
|
|
|
n = len(embeddings)
|
|
|
|
for i in range(n-1):
|
|
|
|
for j in range(i+1, n):
|
|
|
|
embedding1 = np.array(embeddings[i])
|
|
|
|
embedding2 = np.array(embeddings[j])
|
|
|
|
if context.debug:
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"embedding1: {embedding1[-8:]}")
|
|
|
|
print(f"embedding2: {embedding2[-8:]}")
|
2024-03-07 10:41:53 +01:00
|
|
|
similarity = np.dot(embedding1, embedding2) / (np.linalg.norm(embedding1) * np.linalg.norm(embedding2))
|
|
|
|
msg = f"Similarity between {i} and {j}: {similarity:.10f}"
|
|
|
|
if context.debug:
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"{msg}")
|
2024-03-07 10:41:53 +01:00
|
|
|
assert np.isclose(similarity, 1.0, rtol=1e-05, atol=1e-08, equal_nan=False), msg
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
|
|
|
|
@step('embeddings are generated')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_assert_embeddings(context):
|
2024-03-07 10:41:53 +01:00
|
|
|
assert context.n_prompts == len(context.embeddings), (f"unexpected response:\n"
|
|
|
|
f"context.n_prompts={context.n_prompts}\n"
|
|
|
|
f"context.embeddings={context.embeddings}")
|
|
|
|
for embedding in context.embeddings:
|
|
|
|
assert_embeddings(embedding)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('an OAI compatible embeddings computation request for')
|
2024-02-24 19:16:04 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_oai_compute_embeddings(context):
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = 1
|
2024-05-01 17:52:55 +02:00
|
|
|
context.embeddings = await request_oai_embeddings(context_text(context), None,
|
2024-02-24 19:16:04 +01:00
|
|
|
base_url=context.base_url,
|
|
|
|
user_api_key=context.user_api_key,
|
|
|
|
model=context.model)
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('an OAI compatible embeddings computation request for multiple inputs')
|
2024-02-24 19:16:04 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_oai_compute_embeddings_multiple_inputs(context):
|
2024-05-01 17:52:55 +02:00
|
|
|
context.embeddings = await request_oai_embeddings(context.prompts, None,
|
2024-02-24 19:16:04 +01:00
|
|
|
base_url=context.base_url,
|
|
|
|
user_api_key=context.user_api_key,
|
|
|
|
model=context.model)
|
2024-03-07 10:41:53 +01:00
|
|
|
context.prompts.clear()
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('concurrent embedding requests')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete()
|
|
|
|
async def step_concurrent_embedding_requests(context):
|
2024-02-24 19:16:04 +01:00
|
|
|
await concurrent_requests(context,
|
|
|
|
request_embedding,
|
|
|
|
# prompt is inserted automatically
|
|
|
|
base_url=context.base_url)
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('concurrent OAI embedding requests')
|
2024-02-24 19:16:04 +01:00
|
|
|
@async_run_until_complete()
|
|
|
|
async def step_concurrent_oai_embedding_requests(context):
|
|
|
|
await concurrent_requests(context,
|
|
|
|
request_oai_embeddings,
|
|
|
|
# prompt is inserted automatically
|
|
|
|
base_url=context.base_url,
|
|
|
|
async_client=True,
|
|
|
|
model=context.model)
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('all embeddings are generated')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete()
|
|
|
|
async def all_embeddings_are_generated(context):
|
|
|
|
n_embedding_requests = await gather_tasks_results(context)
|
2024-03-07 10:41:53 +01:00
|
|
|
assert n_embedding_requests == context.n_prompts
|
2024-02-24 12:28:55 +01:00
|
|
|
for i in range(n_embedding_requests):
|
2024-03-07 10:41:53 +01:00
|
|
|
assert_embeddings(context.tasks_result.pop().pop())
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-05-08 14:27:58 +02:00
|
|
|
@step('adding special tokens')
|
|
|
|
def step_tokenize_set_add_special(context):
|
|
|
|
context.tokenize_add_special = True
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('tokenizing')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_tokenize(context):
|
2024-03-10 18:17:47 +01:00
|
|
|
context.tokenized_text = context_text(context)
|
2024-02-24 12:28:55 +01:00
|
|
|
async with aiohttp.ClientSession() as session:
|
2024-05-08 14:27:58 +02:00
|
|
|
tokenize_args = {
|
|
|
|
"content": context.tokenized_text,
|
|
|
|
}
|
|
|
|
if getattr(context, 'tokenize_add_special', None) is not None:
|
|
|
|
tokenize_args['add_special'] = context.tokenize_add_special
|
2024-02-24 12:28:55 +01:00
|
|
|
async with session.post(f'{context.base_url}/tokenize',
|
2024-05-08 14:27:58 +02:00
|
|
|
json=tokenize_args) as response:
|
2024-02-24 12:28:55 +01:00
|
|
|
assert response.status == 200
|
|
|
|
tokenize_json = await response.json()
|
|
|
|
context.tokens = tokenize_json['tokens']
|
|
|
|
|
|
|
|
|
2024-05-08 14:27:58 +02:00
|
|
|
@step('tokens can be detokenized')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_detokenize(context):
|
|
|
|
assert len(context.tokens) > 0
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(f'{context.base_url}/detokenize',
|
|
|
|
json={
|
|
|
|
"tokens": context.tokens,
|
|
|
|
}) as response:
|
|
|
|
assert response.status == 200
|
|
|
|
detokenize_json = await response.json()
|
|
|
|
# SPM tokenizer adds a whitespace prefix: https://github.com/google/sentencepiece/issues/15
|
|
|
|
assert context.tokenized_text == detokenize_json['content'].strip()
|
|
|
|
|
|
|
|
|
2024-05-08 14:27:58 +02:00
|
|
|
@step('tokens begin with BOS')
|
|
|
|
def step_strings_for_tokenization(context):
|
|
|
|
assert context.tokens[0] == context.bos
|
|
|
|
|
|
|
|
|
|
|
|
@step('tokens do not begin with BOS')
|
|
|
|
def step_strings_for_tokenization(context):
|
|
|
|
assert context.tokens[0] != context.bos
|
|
|
|
|
|
|
|
|
|
|
|
@step('first token is removed')
|
|
|
|
def step_strings_for_tokenization(context):
|
|
|
|
context.tokens = context.tokens[1:]
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('an OPTIONS request is sent from {origin}')
|
2024-02-24 12:28:55 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_options_request(context, origin):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
2024-03-09 11:27:53 +01:00
|
|
|
headers = {'Authorization': f'Bearer {context.user_api_key}', 'Origin': origin}
|
2024-02-24 12:28:55 +01:00
|
|
|
async with session.options(f'{context.base_url}/v1/chat/completions',
|
2024-03-09 11:27:53 +01:00
|
|
|
headers=headers) as response:
|
2024-02-24 12:28:55 +01:00
|
|
|
assert response.status == 200
|
|
|
|
context.options_response = response
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('CORS header {cors_header} is set to {cors_header_value}')
|
2024-02-24 12:28:55 +01:00
|
|
|
def step_check_options_header_value(context, cors_header, cors_header_value):
|
|
|
|
assert context.options_response.headers[cors_header] == cors_header_value
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('prometheus metrics are exposed')
|
2024-02-25 13:49:43 +01:00
|
|
|
@async_run_until_complete
|
|
|
|
async def step_prometheus_metrics_exported(context):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with await session.get(f'{context.base_url}/metrics') as metrics_response:
|
|
|
|
assert metrics_response.status == 200
|
|
|
|
assert metrics_response.headers['Content-Type'] == "text/plain; version=0.0.4"
|
|
|
|
metrics_raw = await metrics_response.text()
|
|
|
|
metric_exported = False
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.debug:
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"/metrics answer:\n{metrics_raw}")
|
2024-03-08 12:25:04 +01:00
|
|
|
context.metrics = {}
|
2024-02-25 13:49:43 +01:00
|
|
|
for metric in parser.text_string_to_metric_families(metrics_raw):
|
|
|
|
match metric.name:
|
|
|
|
case "llamacpp:kv_cache_usage_ratio":
|
|
|
|
assert len(metric.samples) > 0
|
|
|
|
metric_exported = True
|
2024-03-08 12:25:04 +01:00
|
|
|
context.metrics[metric.name] = metric
|
|
|
|
assert int(metrics_response.headers["Process-Start-Time-Unix"]) > 0, "no header process start time"
|
2024-02-25 13:49:43 +01:00
|
|
|
assert metric_exported, "No metrics exported"
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('metric {metric_name} is {metric_value:d}')
|
2024-03-08 12:25:04 +01:00
|
|
|
def step_assert_metric_value(context, metric_name, metric_value):
|
|
|
|
if metric_name not in context.metrics:
|
|
|
|
assert False, f"no metric {metric_name} in {context.metrics.keys()}"
|
|
|
|
assert context.metrics[metric_name].samples[0].value == metric_value, f"metric: {context.metrics[metric_name]}"
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('available models')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_available_models(context):
|
|
|
|
# openai client always expects an api_key
|
|
|
|
openai.api_key = context.user_api_key if context.user_api_key is not None else 'nope'
|
|
|
|
openai.api_base = f'{context.base_url}/v1'
|
|
|
|
context.models = openai.Model.list().data
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('{n_model:d} models are supported')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_supported_models(context, n_model):
|
|
|
|
if context.debug:
|
|
|
|
print("server models available:", context.models)
|
|
|
|
assert len(context.models) == n_model
|
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
@step('model {i_model:d} is {param} {preposition} {param_value}')
|
2024-03-02 22:00:14 +01:00
|
|
|
def step_supported_models(context, i_model, param, preposition, param_value):
|
|
|
|
assert i_model < len(context.models)
|
|
|
|
model = context.models[i_model]
|
|
|
|
|
|
|
|
param_value = param_value.split(' ', 1)[0]
|
|
|
|
match param:
|
|
|
|
case 'identified':
|
|
|
|
value = model.id
|
|
|
|
case 'trained':
|
|
|
|
value = str(model.meta.n_ctx_train)
|
|
|
|
case _:
|
|
|
|
assert False, "param {param} not supported"
|
|
|
|
assert param_value == value, f"model param {param} {value} != {param_value}"
|
|
|
|
|
|
|
|
|
2024-02-24 19:16:04 +01:00
|
|
|
async def concurrent_requests(context, f_completion, *args, **kwargs):
|
2024-03-07 10:41:53 +01:00
|
|
|
context.n_prompts = len(context.prompts)
|
2024-02-24 12:28:55 +01:00
|
|
|
if context.debug:
|
2024-03-07 10:41:53 +01:00
|
|
|
print(f"starting {context.n_prompts} concurrent completion requests...")
|
|
|
|
assert context.n_prompts > 0
|
2024-05-01 17:52:55 +02:00
|
|
|
seeds = await completions_seed(context)
|
2024-03-07 10:41:53 +01:00
|
|
|
for prompt_no in range(context.n_prompts):
|
2024-05-01 17:52:55 +02:00
|
|
|
shifted_args = [context.prompts.pop(), seeds[prompt_no], *args]
|
2024-02-24 12:28:55 +01:00
|
|
|
context.concurrent_tasks.append(asyncio.create_task(f_completion(*shifted_args, **kwargs)))
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
|
2024-04-08 14:43:30 +02:00
|
|
|
@step('the slot {slot_id:d} is saved with filename "{filename}"')
|
|
|
|
@async_run_until_complete
|
|
|
|
async def step_save_slot(context, slot_id, filename):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(f'{context.base_url}/slots/{slot_id}?action=save',
|
|
|
|
json={"filename": filename},
|
|
|
|
headers={"Content-Type": "application/json"}) as response:
|
|
|
|
context.response = response
|
|
|
|
|
|
|
|
|
|
|
|
@step('the slot {slot_id:d} is restored with filename "{filename}"')
|
|
|
|
@async_run_until_complete
|
|
|
|
async def step_restore_slot(context, slot_id, filename):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(f'{context.base_url}/slots/{slot_id}?action=restore',
|
|
|
|
json={"filename": filename},
|
|
|
|
headers={"Content-Type": "application/json"}) as response:
|
|
|
|
context.response = response
|
|
|
|
|
|
|
|
|
|
|
|
@step('the slot {slot_id:d} is erased')
|
|
|
|
@async_run_until_complete
|
|
|
|
async def step_erase_slot(context, slot_id):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(f'{context.base_url}/slots/{slot_id}?action=erase',
|
|
|
|
headers={"Content-Type": "application/json"}) as response:
|
|
|
|
context.response = response
|
|
|
|
|
|
|
|
|
|
|
|
@step('the server responds with status code {status_code:d}')
|
|
|
|
def step_server_responds_with_status_code(context, status_code):
|
|
|
|
assert context.response.status == status_code
|
|
|
|
|
|
|
|
|
2024-02-24 12:28:55 +01:00
|
|
|
async def request_completion(prompt,
|
2024-05-01 17:52:55 +02:00
|
|
|
seed,
|
2024-02-24 12:28:55 +01:00
|
|
|
base_url,
|
|
|
|
debug=False,
|
2024-03-02 22:00:14 +01:00
|
|
|
prompt_prefix=None,
|
|
|
|
prompt_suffix=None,
|
2024-02-24 12:28:55 +01:00
|
|
|
n_predict=None,
|
2024-04-08 14:43:30 +02:00
|
|
|
cache_prompt=False,
|
|
|
|
id_slot=None,
|
2024-02-24 12:28:55 +01:00
|
|
|
expect_api_error=None,
|
2024-05-01 17:52:55 +02:00
|
|
|
user_api_key=None,
|
|
|
|
temperature=None):
|
2024-02-24 12:28:55 +01:00
|
|
|
if debug:
|
|
|
|
print(f"Sending completion request: {prompt}")
|
|
|
|
origin = "my.super.domain"
|
|
|
|
headers = {
|
|
|
|
'Origin': origin
|
|
|
|
}
|
|
|
|
if user_api_key is not None:
|
|
|
|
if debug:
|
|
|
|
print(f"Set user_api_key: {user_api_key}")
|
|
|
|
headers['Authorization'] = f'Bearer {user_api_key}'
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(f'{base_url}/completion',
|
|
|
|
json={
|
2024-03-02 22:00:14 +01:00
|
|
|
"input_prefix": prompt_prefix,
|
2024-02-24 12:28:55 +01:00
|
|
|
"prompt": prompt,
|
2024-03-02 22:00:14 +01:00
|
|
|
"input_suffix": prompt_suffix,
|
|
|
|
"n_predict": n_predict if n_predict is not None else -1,
|
2024-04-08 14:43:30 +02:00
|
|
|
"cache_prompt": cache_prompt,
|
|
|
|
"id_slot": id_slot,
|
2024-05-01 17:52:55 +02:00
|
|
|
"seed": seed if seed is not None else 42,
|
|
|
|
"temperature": temperature if temperature is not None else "0.8f",
|
2024-02-24 12:28:55 +01:00
|
|
|
},
|
2024-03-02 22:00:14 +01:00
|
|
|
headers=headers,
|
|
|
|
timeout=3600) as response:
|
2024-02-24 12:28:55 +01:00
|
|
|
if expect_api_error is None or not expect_api_error:
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.headers['Access-Control-Allow-Origin'] == origin
|
|
|
|
return await response.json()
|
|
|
|
else:
|
|
|
|
return response.status
|
|
|
|
|
|
|
|
|
|
|
|
async def oai_chat_completions(user_prompt,
|
2024-05-01 17:52:55 +02:00
|
|
|
seed,
|
2024-02-24 12:28:55 +01:00
|
|
|
system_prompt,
|
|
|
|
base_url,
|
2024-02-28 09:39:15 +01:00
|
|
|
base_path,
|
2024-02-24 12:28:55 +01:00
|
|
|
async_client,
|
|
|
|
debug=False,
|
|
|
|
model=None,
|
|
|
|
n_predict=None,
|
|
|
|
enable_streaming=None,
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
response_format=None,
|
2024-02-24 12:28:55 +01:00
|
|
|
user_api_key=None,
|
|
|
|
expect_api_error=None):
|
|
|
|
if debug:
|
|
|
|
print(f"Sending OAI Chat completions request: {user_prompt}")
|
|
|
|
# openai client always expects an api key
|
|
|
|
user_api_key = user_api_key if user_api_key is not None else 'nope'
|
2024-03-02 22:00:14 +01:00
|
|
|
seed = seed if seed is not None else 42
|
2024-02-24 12:28:55 +01:00
|
|
|
enable_streaming = enable_streaming if enable_streaming is not None else False
|
|
|
|
payload = {
|
|
|
|
"messages": [
|
|
|
|
{
|
|
|
|
"role": "system",
|
|
|
|
"content": system_prompt,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"role": "user",
|
|
|
|
"content": user_prompt,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"model": model,
|
|
|
|
"max_tokens": n_predict,
|
|
|
|
"stream": enable_streaming,
|
|
|
|
"seed": seed
|
|
|
|
}
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
if response_format is not None:
|
|
|
|
payload['response_format'] = response_format
|
2024-02-24 12:28:55 +01:00
|
|
|
completion_response = {
|
|
|
|
'content': '',
|
|
|
|
'timings': {
|
2024-03-09 10:30:04 +01:00
|
|
|
'predicted_n': 0,
|
|
|
|
'prompt_n': 0
|
2024-02-24 12:28:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if async_client:
|
|
|
|
origin = 'llama.cpp'
|
|
|
|
headers = {'Authorization': f'Bearer {user_api_key}', 'Origin': origin}
|
|
|
|
async with aiohttp.ClientSession() as session:
|
2024-02-28 09:39:15 +01:00
|
|
|
async with session.post(f'{base_url}{base_path}',
|
2024-02-24 12:28:55 +01:00
|
|
|
json=payload,
|
|
|
|
headers=headers) as response:
|
|
|
|
if enable_streaming:
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.headers['Access-Control-Allow-Origin'] == origin
|
|
|
|
assert response.headers['Content-Type'] == "text/event-stream"
|
|
|
|
event_received = True
|
|
|
|
while event_received:
|
|
|
|
event_received = False
|
|
|
|
async for line_in_bytes in response.content:
|
|
|
|
line = line_in_bytes.decode('utf8')
|
|
|
|
line = line.rstrip('\n').rstrip('\r')
|
|
|
|
if line == '':
|
|
|
|
continue
|
|
|
|
event_data = line.split(': ', 1)
|
|
|
|
assert event_data[0] == 'data', f'Bad event code received: ```{event_data}```'
|
|
|
|
chunk_raw = event_data[1]
|
|
|
|
|
|
|
|
chunk = json.loads(chunk_raw)
|
|
|
|
assert len(chunk['choices']) == 1, f"no choices provided, line ```{line}```"
|
|
|
|
delta = chunk['choices'][0]['delta']
|
|
|
|
if 'content' in delta:
|
|
|
|
completion_response['content'] += delta['content']
|
|
|
|
completion_response['timings']['predicted_n'] += 1
|
|
|
|
else:
|
|
|
|
if expect_api_error is None or not expect_api_error:
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.headers['Access-Control-Allow-Origin'] == origin
|
|
|
|
assert response.headers['Content-Type'] == "application/json; charset=utf-8"
|
|
|
|
chat_completion_raw = await response.json()
|
|
|
|
completion_response = {
|
|
|
|
'content': chat_completion_raw['choices'][0]['message'],
|
|
|
|
'timings': {
|
2024-03-09 10:30:04 +01:00
|
|
|
'predicted_n': chat_completion_raw['usage']['completion_tokens'],
|
|
|
|
'prompt_n': chat_completion_raw['usage']['prompt_tokens']
|
2024-02-24 12:28:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return response.status
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
openai.api_key = user_api_key
|
2024-02-28 09:39:15 +01:00
|
|
|
openai.api_base = f'{base_url}{base_path}'
|
2024-02-24 12:28:55 +01:00
|
|
|
chat_completion = openai.Completion.create(
|
|
|
|
messages=payload['messages'],
|
|
|
|
model=model,
|
|
|
|
max_tokens=n_predict,
|
|
|
|
stream=enable_streaming,
|
json-schema-to-grammar improvements (+ added to server) (#5978)
* json: fix arrays (disallow `[,1]`)
* json: support tuple types (`[number, string]`)
* json: support additionalProperties (`{[k: string]: [string,number][]}`)
* json: support required / optional properties
* json: add support for pattern
* json: resolve $ref (and support https schema urls)
* json: fix $ref resolution
* join: support union types (mostly for nullable types I think)
* json: support allOf + nested anyOf
* json: support any (`{}` or `{type: object}`)
* json: fix merge
* json: temp fix for escapes
* json: spaces in output and unrestricted output spaces
* json: add typings
* json:fix typo
* Create ts-type-to-grammar.sh
* json: fix _format_literal (json.dumps already escapes quotes)
* json: merge lit sequences and handle negatives
{"type": "string", "pattern": "^({\"question\": \"[^\"]+\", \"response\": \"[^\"]+\"}\\n)+$"}
* json: handle pattern repetitions
* Update json-schema-to-grammar.mjs
* Create regex-to-grammar.py
* json: extract repeated regexp patterns to subrule
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* json: handle schema from pydantic Optional fields
* Update json-schema-to-grammar.py
* Update json-schema-to-grammar.py
* Update ts-type-to-grammar.sh
* Update ts-type-to-grammar.sh
* json: simplify nullable fields handling
* json: accept duplicate identical rules
* json: revert space to 1 at most
* json: reuse regexp pattern subrules
* json: handle uuid string format
* json: fix literal escapes
* json: add --allow-fetch
* json: simplify range escapes
* json: support negative ranges in patterns
* Delete commit.txt
* json: custom regex parser, adds dot support & JS-portable
* json: rm trailing spaces
* Update json-schema-to-grammar.mjs
* json: updated server & chat `( cd examples/server && ./deps.sh )`
* json: port fixes from mjs to python
* Update ts-type-to-grammar.sh
* json: support prefixItems alongside array items
* json: add date format + fix uuid
* json: add date, time, date-time formats
* json: preserve order of props from TS defs
* json: port schema converter to C++, wire in ./server
* json: nits
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* Update json-schema-to-grammar.cpp
* json: fix mjs implementation + align outputs
* Update json-schema-to-grammar.mjs.hpp
* json: test C++, JS & Python versions
* json: nits + regen deps
* json: cleanup test
* json: revert from c++17 to 11
* json: nit fixes
* json: dirty include for test
* json: fix zig build
* json: pass static command to std::system in tests (fixed temp files)
* json: fix top-level $refs
* json: don't use c++20 designated initializers
* nit
* json: basic support for reserved names `{number:{number:{root:number}}}`
* Revamp test cmake to allow args (WORKING_DIRECTORY needed for JSON test)
* json: re-ran server deps.sh
* json: simplify test
* json: support mix of additional props & required/optional
* json: add tests for some expected failures
* json: fix type=const in c++, add failure expectations for non-str const&enum
* json: test (& simplify output of) empty schema
* json: check parsing in test + fix value & string refs
* json: add server tests for OAI JSON response_format
* json: test/fix top-level anyOf
* json: improve grammar parsing failures
* json: test/fix additional props corner cases
* json: fix string patterns (was missing quotes)
* json: ws nit
* json: fix json handling in server when there's no response_format
* json: catch schema conversion errors in server
* json: don't complain about unknown format type in server if unset
* json: cleaner build of test
* json: create examples/json-schema-pydantic-example.py
* json: fix date pattern
* json: move json.hpp & json-schema-to-grammar.{cpp,h} to common
* json: indent 4 spaces
* json: fix naming of top-level c++ function (+ drop unused one)
* json: avoid using namespace std
* json: fix zig build
* Update server.feature
* json: iostream -> fprintf
* json: space before & refs for consistency
* json: nits
2024-03-21 12:50:43 +01:00
|
|
|
response_format=payload.get('response_format'),
|
2024-02-24 12:28:55 +01:00
|
|
|
seed=seed
|
|
|
|
)
|
2024-03-11 10:56:41 +01:00
|
|
|
except openai.error.AuthenticationError as e:
|
2024-02-24 12:28:55 +01:00
|
|
|
if expect_api_error is not None and expect_api_error:
|
|
|
|
return 401
|
|
|
|
else:
|
|
|
|
assert False, f'error raised: {e}'
|
|
|
|
|
|
|
|
if enable_streaming:
|
|
|
|
for chunk in chat_completion:
|
|
|
|
assert len(chunk.choices) == 1
|
|
|
|
delta = chunk.choices[0].delta
|
|
|
|
if 'content' in delta:
|
|
|
|
completion_response['content'] += delta['content']
|
|
|
|
completion_response['timings']['predicted_n'] += 1
|
2024-03-09 10:30:04 +01:00
|
|
|
completion_response['truncated'] = chunk.choices[0].finish_reason != 'stop'
|
2024-02-24 12:28:55 +01:00
|
|
|
else:
|
|
|
|
assert len(chat_completion.choices) == 1
|
|
|
|
completion_response = {
|
|
|
|
'content': chat_completion.choices[0].message.content,
|
|
|
|
'timings': {
|
2024-03-09 10:30:04 +01:00
|
|
|
'predicted_n': chat_completion.usage.completion_tokens,
|
|
|
|
'prompt_n': chat_completion.usage.prompt_tokens
|
|
|
|
},
|
|
|
|
'truncated': chat_completion.choices[0].finish_reason != 'stop'
|
2024-02-24 12:28:55 +01:00
|
|
|
}
|
|
|
|
if debug:
|
|
|
|
print("OAI response formatted to llama.cpp:", completion_response)
|
|
|
|
return completion_response
|
|
|
|
|
|
|
|
|
2024-05-01 17:52:55 +02:00
|
|
|
async def request_embedding(content, seed, base_url=None):
|
2024-02-24 12:28:55 +01:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(f'{base_url}/embedding',
|
|
|
|
json={
|
|
|
|
"content": content,
|
|
|
|
}) as response:
|
|
|
|
assert response.status == 200
|
|
|
|
response_json = await response.json()
|
2024-03-07 10:41:53 +01:00
|
|
|
return [response_json['embedding']]
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
2024-05-01 17:52:55 +02:00
|
|
|
async def request_oai_embeddings(input, seed,
|
2024-02-24 19:16:04 +01:00
|
|
|
base_url=None, user_api_key=None,
|
|
|
|
model=None, async_client=False):
|
|
|
|
# openai client always expects an api_key
|
|
|
|
user_api_key = user_api_key if user_api_key is not None else 'nope'
|
|
|
|
if async_client:
|
|
|
|
origin = 'llama.cpp'
|
2024-03-07 10:41:53 +01:00
|
|
|
headers=[]
|
2024-02-24 19:16:04 +01:00
|
|
|
if user_api_key is not None:
|
|
|
|
headers = {'Authorization': f'Bearer {user_api_key}', 'Origin': origin}
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(f'{base_url}/v1/embeddings',
|
|
|
|
json={
|
|
|
|
"input": input,
|
|
|
|
"model": model,
|
|
|
|
},
|
2024-03-07 10:41:53 +01:00
|
|
|
headers=headers,
|
|
|
|
timeout=3600) as response:
|
2024-02-24 19:16:04 +01:00
|
|
|
assert response.status == 200, f"received status code not expected: {response.status}"
|
|
|
|
assert response.headers['Access-Control-Allow-Origin'] == origin
|
|
|
|
assert response.headers['Content-Type'] == "application/json; charset=utf-8"
|
|
|
|
response_json = await response.json()
|
|
|
|
assert response_json['model'] == model, f"invalid model received: {response_json['model']}"
|
|
|
|
assert response_json['object'] == 'list'
|
2024-03-07 10:41:53 +01:00
|
|
|
if isinstance(input, collections.abc.Sequence):
|
|
|
|
embeddings = []
|
|
|
|
for an_oai_embeddings in response_json['data']:
|
|
|
|
embeddings.append(an_oai_embeddings['embedding'])
|
|
|
|
else:
|
|
|
|
embeddings = [response_json['data']['embedding']]
|
|
|
|
return embeddings
|
2024-02-24 19:16:04 +01:00
|
|
|
else:
|
|
|
|
openai.api_key = user_api_key
|
|
|
|
openai.api_base = f'{base_url}/v1'
|
|
|
|
oai_embeddings = openai.Embedding.create(
|
|
|
|
model=model,
|
|
|
|
input=input,
|
|
|
|
)
|
|
|
|
|
|
|
|
if isinstance(input, collections.abc.Sequence):
|
|
|
|
embeddings = []
|
|
|
|
for an_oai_embeddings in oai_embeddings.data:
|
|
|
|
embeddings.append(an_oai_embeddings.embedding)
|
|
|
|
else:
|
2024-03-07 10:41:53 +01:00
|
|
|
embeddings = [oai_embeddings.data.embedding]
|
2024-02-24 19:16:04 +01:00
|
|
|
return embeddings
|
|
|
|
|
|
|
|
|
2024-02-24 12:28:55 +01:00
|
|
|
def assert_n_tokens_predicted(completion_response, expected_predicted_n=None, re_content=None):
|
|
|
|
content = completion_response['content']
|
|
|
|
n_predicted = completion_response['timings']['predicted_n']
|
|
|
|
assert len(content) > 0, "no token predicted"
|
2024-03-02 22:00:14 +01:00
|
|
|
if re_content is not None:
|
|
|
|
p = re.compile(re_content, flags=RegexFlag.IGNORECASE | RegexFlag.MULTILINE | RegexFlag.DOTALL)
|
|
|
|
matches = p.finditer(content)
|
|
|
|
last_match = 0
|
|
|
|
highlighted = ''
|
|
|
|
for match in matches:
|
|
|
|
start, end = match.span()
|
|
|
|
highlighted += content[last_match: start]
|
|
|
|
highlighted += '\x1b[33m'
|
|
|
|
highlighted += content[start: end]
|
|
|
|
highlighted += '\x1b[0m'
|
|
|
|
last_match = end
|
|
|
|
highlighted += content[last_match:]
|
|
|
|
if 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON':
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"Checking completion response: {highlighted}")
|
2024-03-02 22:00:14 +01:00
|
|
|
assert last_match > 0, f'/{re_content}/ must match ```{highlighted}```'
|
|
|
|
if expected_predicted_n and expected_predicted_n > 0:
|
2024-02-24 12:28:55 +01:00
|
|
|
assert n_predicted == expected_predicted_n, (f'invalid number of tokens predicted:'
|
|
|
|
f' {n_predicted} <> {expected_predicted_n}')
|
2024-03-02 22:00:14 +01:00
|
|
|
|
2024-04-24 11:08:36 +02:00
|
|
|
def assert_all_predictions_equal(completion_responses):
|
|
|
|
if 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON':
|
2024-05-01 17:52:55 +02:00
|
|
|
for i, response_i in enumerate(completion_responses):
|
|
|
|
content_i = response_i['content']
|
|
|
|
print(f"content {i}: {content_i}")
|
|
|
|
for i, response_i in enumerate(completion_responses):
|
|
|
|
content_i = response_i['content']
|
|
|
|
for j, response_j in enumerate(completion_responses):
|
|
|
|
if i == j:
|
|
|
|
continue
|
|
|
|
content_j = response_j['content']
|
|
|
|
assert content_i == content_j, "contents not equal"
|
|
|
|
|
|
|
|
|
|
|
|
def assert_all_predictions_different(completion_responses):
|
|
|
|
if 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON':
|
|
|
|
for i, response_i in enumerate(completion_responses):
|
|
|
|
content_i = response_i['content']
|
|
|
|
print(f"content {i}: {content_i}")
|
|
|
|
for i, response_i in enumerate(completion_responses):
|
|
|
|
content_i = response_i['content']
|
|
|
|
for j, response_j in enumerate(completion_responses):
|
|
|
|
if i == j:
|
|
|
|
continue
|
|
|
|
content_j = response_j['content']
|
|
|
|
assert content_i != content_j, "contents not different"
|
2024-04-24 11:08:36 +02:00
|
|
|
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
async def gather_tasks_results(context):
|
|
|
|
n_tasks = len(context.concurrent_tasks)
|
|
|
|
if context.debug:
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"Waiting for all {n_tasks} tasks results...")
|
2024-02-24 12:28:55 +01:00
|
|
|
for task_no in range(n_tasks):
|
|
|
|
context.tasks_result.append(await context.concurrent_tasks.pop())
|
|
|
|
n_completions = len(context.tasks_result)
|
|
|
|
return n_completions
|
|
|
|
|
|
|
|
|
|
|
|
async def wait_for_health_status(context,
|
|
|
|
base_url,
|
|
|
|
expected_http_status_code,
|
|
|
|
expected_health_status,
|
2024-03-02 22:00:14 +01:00
|
|
|
timeout=3,
|
2024-02-24 12:28:55 +01:00
|
|
|
params=None,
|
|
|
|
slots_idle=None,
|
|
|
|
slots_processing=None,
|
|
|
|
expected_slots=None):
|
|
|
|
if context.debug:
|
2024-03-20 06:33:49 +01:00
|
|
|
print(f"Starting checking for health for expected_health_status={expected_health_status}")
|
2024-02-24 12:28:55 +01:00
|
|
|
interval = 0.5
|
|
|
|
counter = 0
|
2024-03-14 12:15:39 +01:00
|
|
|
if 'GITHUB_ACTIONS' in os.environ:
|
|
|
|
timeout *= 2
|
|
|
|
|
2024-02-24 12:28:55 +01:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
while True:
|
|
|
|
async with await session.get(f'{base_url}/health', params=params) as health_response:
|
|
|
|
status_code = health_response.status
|
|
|
|
health = await health_response.json()
|
|
|
|
if context.debug:
|
|
|
|
print(f"HEALTH - response for expected health status='{expected_health_status}' on "
|
2024-03-02 22:00:14 +01:00
|
|
|
f"'{base_url}/health'?{params} is {health}\n")
|
2024-02-24 12:28:55 +01:00
|
|
|
if (status_code == expected_http_status_code
|
|
|
|
and health['status'] == expected_health_status
|
|
|
|
and (slots_idle is None or health['slots_idle'] == slots_idle)
|
|
|
|
and (slots_processing is None or health['slots_processing'] == slots_processing)):
|
|
|
|
if expected_slots is not None:
|
|
|
|
assert_slots_status(health['slots'], expected_slots)
|
|
|
|
return
|
|
|
|
if (status_code == expected_http_status_code
|
|
|
|
and health['status'] == expected_health_status
|
|
|
|
and (slots_idle is None or health['slots_idle'] == slots_idle)
|
|
|
|
and (slots_processing is None or health['slots_processing'] == slots_processing)):
|
|
|
|
if expected_slots is not None:
|
|
|
|
assert_slots_status(health['slots'], expected_slots)
|
|
|
|
return
|
|
|
|
await asyncio.sleep(interval)
|
|
|
|
|
|
|
|
counter += interval
|
|
|
|
if counter >= timeout:
|
|
|
|
# Sometimes health requests are triggered after completions are predicted
|
|
|
|
if expected_http_status_code == 503:
|
|
|
|
if len(context.tasks_result) == 0:
|
|
|
|
print("\x1b[5;37;43mWARNING: forcing concurrent tasks,"
|
2024-03-02 22:00:14 +01:00
|
|
|
" busy health check missed, probably too fast inference\x1b[0m\n")
|
2024-02-24 12:28:55 +01:00
|
|
|
n_completions = await gather_tasks_results(context)
|
|
|
|
if n_completions > 0:
|
|
|
|
return
|
|
|
|
|
2024-02-25 22:48:33 +01:00
|
|
|
assert False, f'{expected_health_status} timeout exceeded {counter}s>={timeout}'
|
2024-02-24 12:28:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def assert_embeddings(embeddings):
|
|
|
|
assert len(embeddings) > 0
|
|
|
|
embeddings_computed = False
|
|
|
|
for emb in embeddings:
|
2024-03-07 10:41:53 +01:00
|
|
|
if not isinstance(emb, float):
|
|
|
|
assert False, f"Bad embeddings: {embeddings}"
|
2024-02-24 12:28:55 +01:00
|
|
|
if emb != 0:
|
|
|
|
embeddings_computed = True
|
|
|
|
assert embeddings_computed, f"Embeddings: {embeddings}"
|
|
|
|
|
|
|
|
|
|
|
|
async def request_slots_status(context, expected_slots):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with await session.get(f'{context.base_url}/slots') as slots_response:
|
|
|
|
assert slots_response.status == 200
|
|
|
|
slots = await slots_response.json()
|
|
|
|
assert_slots_status(slots, expected_slots)
|
|
|
|
|
|
|
|
|
|
|
|
def assert_slots_status(slots, expected_slots):
|
|
|
|
assert len(slots) == len(expected_slots)
|
|
|
|
for slot_id, (expected, slot) in enumerate(zip(expected_slots, slots)):
|
|
|
|
for key in expected:
|
|
|
|
assert expected[key] == slot[key], (f"invalid slot {slot_id}"
|
|
|
|
f" expected[{key}] != slot[{key}]"
|
|
|
|
f" = {expected[key]} != {slot[key]}")
|
|
|
|
|
|
|
|
|
2024-05-01 17:52:55 +02:00
|
|
|
async def completions_seed(context, num_seeds=None):
|
|
|
|
if hasattr(context, "seed") and context.seed is not None:
|
|
|
|
assert len(context.seed) == context.n_prompts
|
|
|
|
if num_seeds is None:
|
|
|
|
num_seeds = context.n_prompts
|
|
|
|
assert num_seeds <= context.n_prompts
|
|
|
|
seeds = context.seed[:num_seeds]
|
|
|
|
context.seed = context.seed[num_seeds:] if num_seeds < context.n_prompts else None
|
|
|
|
return seeds
|
|
|
|
|
|
|
|
if hasattr(context, "server_seed") and context.server_seed is not None:
|
|
|
|
if num_seeds is None:
|
|
|
|
return [context.server_seed] * context.n_prompts
|
|
|
|
else:
|
|
|
|
return [context.server_seed] * num_seeds
|
|
|
|
return None
|
2024-03-02 22:00:14 +01:00
|
|
|
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
def context_text(context):
|
|
|
|
return context.text.replace('\r', '')
|
|
|
|
|
|
|
|
|
2024-02-24 12:28:55 +01:00
|
|
|
def start_server_background(context):
|
2024-03-10 18:17:47 +01:00
|
|
|
if os.name == 'nt':
|
|
|
|
context.server_path = '../../../build/bin/Release/server.exe'
|
|
|
|
else:
|
|
|
|
context.server_path = '../../../build/bin/server'
|
2024-02-24 12:28:55 +01:00
|
|
|
if 'LLAMA_SERVER_BIN_PATH' in os.environ:
|
|
|
|
context.server_path = os.environ['LLAMA_SERVER_BIN_PATH']
|
2024-03-10 18:17:47 +01:00
|
|
|
server_listen_addr = context.server_fqdn
|
2024-02-24 12:28:55 +01:00
|
|
|
server_args = [
|
2024-03-10 18:17:47 +01:00
|
|
|
'--host', server_listen_addr,
|
2024-02-24 12:28:55 +01:00
|
|
|
'--port', context.server_port,
|
|
|
|
]
|
2024-03-17 19:12:37 +01:00
|
|
|
if context.model_file:
|
|
|
|
server_args.extend(['--model', context.model_file])
|
|
|
|
if context.model_url:
|
|
|
|
server_args.extend(['--model-url', context.model_url])
|
2024-03-23 18:07:00 +01:00
|
|
|
if context.model_hf_repo:
|
|
|
|
server_args.extend(['--hf-repo', context.model_hf_repo])
|
|
|
|
if context.model_hf_file:
|
|
|
|
server_args.extend(['--hf-file', context.model_hf_file])
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.n_batch:
|
|
|
|
server_args.extend(['--batch-size', context.n_batch])
|
2024-03-13 18:54:21 +01:00
|
|
|
if context.n_ubatch:
|
|
|
|
server_args.extend(['--ubatch-size', context.n_ubatch])
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.n_gpu_layer:
|
|
|
|
server_args.extend(['--n-gpu-layers', context.n_gpu_layer])
|
2024-04-24 11:08:36 +02:00
|
|
|
if context.draft is not None:
|
|
|
|
server_args.extend(['--draft', context.draft])
|
2024-02-24 12:28:55 +01:00
|
|
|
if context.server_continuous_batching:
|
|
|
|
server_args.append('--cont-batching')
|
|
|
|
if context.server_embeddings:
|
|
|
|
server_args.append('--embedding')
|
2024-02-25 13:49:43 +01:00
|
|
|
if context.server_metrics:
|
|
|
|
server_args.append('--metrics')
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.model_alias:
|
2024-02-24 12:28:55 +01:00
|
|
|
server_args.extend(['--alias', context.model_alias])
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.n_ctx:
|
2024-02-24 12:28:55 +01:00
|
|
|
server_args.extend(['--ctx-size', context.n_ctx])
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.n_slots:
|
2024-02-24 12:28:55 +01:00
|
|
|
server_args.extend(['--parallel', context.n_slots])
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.n_server_predict:
|
2024-02-24 12:28:55 +01:00
|
|
|
server_args.extend(['--n-predict', context.n_server_predict])
|
2024-04-08 14:43:30 +02:00
|
|
|
if context.slot_save_path:
|
|
|
|
server_args.extend(['--slot-save-path', context.slot_save_path])
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.server_api_key:
|
2024-02-24 12:28:55 +01:00
|
|
|
server_args.extend(['--api-key', context.server_api_key])
|
2024-03-02 22:00:14 +01:00
|
|
|
if context.n_ga:
|
|
|
|
server_args.extend(['--grp-attn-n', context.n_ga])
|
|
|
|
if context.n_ga_w:
|
|
|
|
server_args.extend(['--grp-attn-w', context.n_ga_w])
|
2024-02-24 12:28:55 +01:00
|
|
|
if context.debug:
|
|
|
|
server_args.append('--verbose')
|
2024-02-25 13:50:32 +01:00
|
|
|
if 'SERVER_LOG_FORMAT_JSON' not in os.environ:
|
|
|
|
server_args.extend(['--log-format', "text"])
|
2024-03-27 20:26:49 +01:00
|
|
|
|
|
|
|
args = [str(arg) for arg in [context.server_path, *server_args]]
|
|
|
|
print(f"bench: starting server with: {' '.join(args)}")
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
flags = 0
|
|
|
|
if 'nt' == os.name:
|
|
|
|
flags |= subprocess.DETACHED_PROCESS
|
|
|
|
flags |= subprocess.CREATE_NEW_PROCESS_GROUP
|
|
|
|
flags |= subprocess.CREATE_NO_WINDOW
|
|
|
|
|
|
|
|
pkwargs = {
|
|
|
|
'creationflags': flags,
|
2024-03-17 19:12:37 +01:00
|
|
|
'stdout': subprocess.PIPE,
|
|
|
|
'stderr': subprocess.PIPE
|
2024-03-10 18:17:47 +01:00
|
|
|
}
|
2024-02-24 12:28:55 +01:00
|
|
|
context.server_process = subprocess.Popen(
|
|
|
|
[str(arg) for arg in [context.server_path, *server_args]],
|
2024-03-10 18:17:47 +01:00
|
|
|
**pkwargs)
|
2024-03-17 19:12:37 +01:00
|
|
|
|
2024-03-27 20:26:49 +01:00
|
|
|
def server_log(in_stream, out_stream):
|
|
|
|
for line in iter(in_stream.readline, b''):
|
|
|
|
print(line.decode('utf-8'), end='', file=out_stream)
|
|
|
|
|
|
|
|
thread_stdout = threading.Thread(target=server_log, args=(context.server_process.stdout, sys.stdout))
|
2024-03-17 19:12:37 +01:00
|
|
|
thread_stdout.start()
|
|
|
|
|
2024-03-27 20:26:49 +01:00
|
|
|
thread_stderr = threading.Thread(target=server_log, args=(context.server_process.stderr, sys.stderr))
|
2024-03-17 19:12:37 +01:00
|
|
|
thread_stderr.start()
|
|
|
|
|
2024-03-10 18:17:47 +01:00
|
|
|
print(f"server pid={context.server_process.pid}, behave pid={os.getpid()}")
|