File size: 1,271 Bytes
bd5bdda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""Send one JSON input using the same template/token path as the evaluation."""
import argparse
import json
from pathlib import Path

import httpx
from transformers import AutoTokenizer

parser = argparse.ArgumentParser()
parser.add_argument('input', type=Path, help='UTF-8 user JSON file')
parser.add_argument('--url', default='http://127.0.0.1:8091')
args = parser.parse_args()
tokenizer = AutoTokenizer.from_pretrained(
    'google/gemma-4-E4B-it', revision='ee0ef6023621cff504d758262d4e04895a5af4a2')
system = Path(__file__).with_name('system_prompt.txt').read_text(encoding='utf-8')
user = args.input.read_text(encoding='utf-8-sig')
json.loads(user)
tokens = tokenizer.apply_chat_template(
    [{'role': 'system', 'content': system}, {'role': 'user', 'content': user}],
    tokenize=True, return_dict=True, add_generation_prompt=True,
    enable_thinking=False)['input_ids']
if len(tokens) + 640 > 2048:
    raise ValueError('Input exceeds the evaluated context budget; shorten input or configure a larger server context.')
response = httpx.post(args.url.rstrip('/') + '/completion', json={
    'prompt': tokens, 'temperature': 0, 'seed': 3407,
    'n_predict': 640, 'cache_prompt': False}, timeout=180)
response.raise_for_status()
print(response.json()['content'])