| import requests, json, time | |
| BASE = 'http://127.0.0.1:7860/api/v1/chat/completions' | |
| headers = {'Content-Type': 'application/json'} | |
| print('Non-streaming example') | |
| payload = { | |
| 'model': 'rwkv-latest', | |
| 'prompt': 'Who is the president of France today?', | |
| 'stream': False, | |
| 'max_tokens': 64, | |
| 'temperature': 0.2, | |
| 'include_usage': True, | |
| } | |
| try: | |
| r = requests.post(BASE, json=payload, timeout=120) | |
| print('Status', r.status_code) | |
| try: | |
| print(json.dumps(r.json(), indent=2)) | |
| except Exception: | |
| print('Non-JSON response:', r.text[:1000]) | |
| except Exception as e: | |
| print('Error in non-stream request:', e) | |
| print('\nTools: calc example') | |
| payload = { | |
| 'model': 'rwkv-latest', | |
| 'prompt': 'Calculate 2+3*4 and explain the result.', | |
| 'stream': False, | |
| 'tools': [{'name': 'calc', 'args': {'expression': '2+3*4'}}], | |
| 'include_usage': True, | |
| } | |
| try: | |
| r = requests.post(BASE, json=payload, timeout=120) | |
| print('Status', r.status_code) | |
| try: | |
| print(json.dumps(r.json(), indent=2)) | |
| except Exception: | |
| print('Non-JSON response:', r.text[:1000]) | |
| except Exception as e: | |
| print('Error in calc tool request:', e) | |
| print('\nTools: web_search example') | |
| payload = { | |
| 'model': 'rwkv-latest', | |
| 'prompt': 'Who is the current president of France?', | |
| 'stream': False, | |
| 'web_search': True, | |
| 'search_top_k': 2, | |
| 'include_usage': True, | |
| } | |
| try: | |
| r = requests.post(BASE, json=payload, timeout=120) | |
| print('Status', r.status_code) | |
| try: | |
| print(json.dumps(r.json(), indent=2)) | |
| except Exception: | |
| print('Non-JSON response:', r.text[:1000]) | |
| except Exception as e: | |
| print('Error in web_search request:', e) | |
| print('\nStreaming example (short)') | |
| payload = { | |
| 'model': 'rwkv-latest:thinking', | |
| 'messages': [{'role': 'user', 'content': 'Explain Newton\'s first law in one sentence.'}], | |
| 'stream': True, | |
| 'max_tokens': 64, | |
| 'temperature': 0.2, | |
| } | |
| try: | |
| r = requests.post(BASE, json=payload, headers=headers, stream=True, timeout=120) | |
| print('Status', r.status_code) | |
| if r.status_code == 200: | |
| for line in r.iter_lines(decode_unicode=True): | |
| if not line: | |
| continue | |
| print('SSE:', line) | |
| if line.strip().endswith('[DONE]'): | |
| break | |
| except Exception as e: | |
| print('Error in streaming request:', e) | |
| print('\nDone tests') | |
| print('\nChecking model listing endpoint') | |
| try: | |
| r = requests.get('http://127.0.0.1:7860/api/v1/models', timeout=10) | |
| print('Models endpoint status', r.status_code) | |
| try: | |
| print(json.dumps(r.json(), indent=2)) | |
| except Exception: | |
| print('Models endpoint returned non-JSON:', r.text[:200]) | |
| except Exception as e: | |
| print('Error calling models endpoint:', e) | |