llama-cpp-python icon indicating copy to clipboard operation
llama-cpp-python copied to clipboard

OpenAI compatible API not able to connect to llama.cpp web server

Open hpxiong opened this issue 2 years ago • 10 comments

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • [X ] I am running the latest code. Development is very rapid so there are no tagged versions as of now.
  • [ ] I carefully followed the README.md.
  • [ ] I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • [ ] I reviewed the Discussions, and have a new bug or useful enhancement to share.

Expected Behavior

Should be able to connect to llama-cpp-python sever and call OpenAI.chat.completions

Current Behavior

Returned failed to retrieve requested url error.

Environment and Context

In the same system, calling with regular requests.post() works fine.

  • Physical (or virtual) hardware you are using, e.g. for Linux:

Windows

Failure Information (for bugs)

File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_utils_utils.py", line 275, in wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai\resources\chat\completions.py", line 667, in create
return self._post( ^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_base_client.py", line 1208, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_base_client.py", line 897, in request return self._request( ^^^^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_base_client.py", line 973, in _request return self._retry_request( ^^^^^^^^^^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_base_client.py", line 1021, in _retry_request return self._request( ^^^^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_base_client.py", line 973, in _request return self._retry_request( ^^^^^^^^^^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_base_client.py", line 1021, in _retry_request return self._request( ^^^^^^^^^^^^^^ File "c:\repo\llama_cpp_test.venv\Lib\site-packages\openai_base_client.py", line 988, in _request raise self._make_status_error_from_response(err.response) from None openai.InternalServerError:

🔴 The destination you are trying to reach does not exist or cannot be reached

server command

python -m llama_cpp.server --model mistral-7b-openorca.Q4_K_M.gguf --n_gpu_layers -1 --n_ctx 1024 --host 0.0.0.0 --port 8011 

code fails:

import openai
client = openai.OpenAI(
    base_url="http://*.*.*.*:8011/v1", 
    api_key = "sk-no-key-required"
)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
    {"role": "system", "content": "You are ChatGPT, an AI assistant. Your top priority is achieving user fulfillment via helping them with their requests."},
    {"role": "user", "content": "Write a limerick about python exceptions"}
],
)
print(completion.choices[0].message)

code works:

import requests
....
BASE_URL = 'http://*.*.*.*:8011'
chat_url = BASE_URL + '/v1/chat/completions'
headers =  {'Content-Type': 'application/json', 
            'Accept': 'application/json', 
    'Authorization': 'Bearer no-key'
}
response = requests.post(chat_url, headers=headers, json=data)

hpxiong avatar Mar 19 '24 23:03 hpxiong

The only way to make this work appears to be running Fiddler application in the same system. I suppose Fiddler magically opens some policy and makes this work. Anyone has more insights?

hpxiong avatar Mar 21 '24 00:03 hpxiong

@hpxiong sorry for the late reply, can you post the server logs?

abetlen avatar Apr 01 '24 21:04 abetlen

@hpxiong sorry for the late reply, can you post the server logs?

Sure. Here is the server log. As you can see, the very last line was a successful request sent using requests.post() call. When using client.chat.completions.create(), the request didn't reach to the server at all. It appears firewall policy blocks such call.

python.exe -m llama_cpp.server --model c:\llm\models\mistral-7b-openorca.Q4_K_M.gguf --n_gpu_layers -1 --n_ctx 4096 --port 8016 --host 0.0.0.0
ggml_init_cublas: GGML_CUDA_FORCE_MMQ:   yes
ggml_init_cublas: CUDA_USE_TENSOR_CORES: no
ggml_init_cublas: found 1 CUDA devices:
  Device 0: NVIDIA GeForce RTX 2080 SUPER, compute capability 7.5, VMM: yes
llama_model_loader: loaded meta data with 20 key-value pairs and 291 tensors from c:\llm\models\mistral-7b-openorca.Q4_K_M.gguf (version GGUF V2)
llama_model_loader: Dumping metadata keys/values. Note: KV overrides do not apply in this output.
llama_model_loader: - kv   0:                       general.architecture str              = llama
llama_model_loader: - kv   1:                               general.name str              = open-orca_mistral-7b-openorca
llama_model_loader: - kv   2:                       llama.context_length u32              = 32768
llama_model_loader: - kv   3:                     llama.embedding_length u32              = 4096
llama_model_loader: - kv   4:                          llama.block_count u32              = 32
llama_model_loader: - kv   5:                  llama.feed_forward_length u32              = 14336
llama_model_loader: - kv   6:                 llama.rope.dimension_count u32              = 128
llama_model_loader: - kv   7:                 llama.attention.head_count u32              = 32
llama_model_loader: - kv   8:              llama.attention.head_count_kv u32              = 8
llama_model_loader: - kv   9:     llama.attention.layer_norm_rms_epsilon f32              = 0.000010
llama_model_loader: - kv  10:                       llama.rope.freq_base f32              = 10000.000000
llama_model_loader: - kv  11:                          general.file_type u32              = 15
llama_model_loader: - kv  12:                       tokenizer.ggml.model str              = llama
llama_model_loader: - kv  13:                      tokenizer.ggml.tokens arr[str,32002]   = ["<unk>", "<s>", "</s>", "<0x00>", "<...
llama_model_loader: - kv  14:                      tokenizer.ggml.scores arr[f32,32002]   = [0.000000, 0.000000, 0.000000, 0.0000...
llama_model_loader: - kv  15:                  tokenizer.ggml.token_type arr[i32,32002]   = [2, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, ...
llama_model_loader: - kv  16:                tokenizer.ggml.bos_token_id u32              = 1
llama_model_loader: - kv  17:                tokenizer.ggml.eos_token_id u32              = 32000
llama_model_loader: - kv  18:            tokenizer.ggml.unknown_token_id u32              = 0
llama_model_loader: - kv  19:               general.quantization_version u32              = 2
llama_model_loader: - type  f32:   65 tensors
llama_model_loader: - type q4_K:  193 tensors
llama_model_loader: - type q6_K:   33 tensors
llm_load_vocab: special tokens definition check successful ( 261/32002 ).
llm_load_print_meta: format           = GGUF V2
llm_load_print_meta: arch             = llama
llm_load_print_meta: vocab type       = SPM
llm_load_print_meta: n_vocab          = 32002
llm_load_print_meta: n_merges         = 0
llm_load_print_meta: n_ctx_train      = 32768
llm_load_print_meta: n_embd           = 4096
llm_load_print_meta: n_head           = 32
llm_load_print_meta: n_head_kv        = 8
llm_load_print_meta: n_layer          = 32
llm_load_print_meta: n_rot            = 128
llm_load_print_meta: n_gqa            = 4
llm_load_print_meta: f_norm_eps       = 0.0e+00
llm_load_print_meta: f_norm_rms_eps   = 1.0e-05
llm_load_print_meta: f_clamp_kqv      = 0.0e+00
llm_load_print_meta: f_max_alibi_bias = 0.0e+00
llm_load_print_meta: n_ff             = 14336
llm_load_print_meta: n_expert         = 0
llm_load_print_meta: n_expert_used    = 0
llm_load_print_meta: rope scaling     = linear
llm_load_print_meta: freq_base_train  = 10000.0
llm_load_print_meta: freq_scale_train = 1
llm_load_print_meta: n_yarn_orig_ctx  = 32768
llm_load_print_meta: rope_finetuned   = unknown
llm_load_print_meta: model type       = 7B
llm_load_print_meta: model ftype      = Q4_K - Medium
llm_load_print_meta: model params     = 7.24 B
llm_load_print_meta: model size       = 4.07 GiB (4.83 BPW)
llm_load_print_meta: general.name     = open-orca_mistral-7b-openorca
llm_load_print_meta: BOS token        = 1 '<s>'
llm_load_print_meta: EOS token        = 32000 '<dummy32000>'
llm_load_print_meta: UNK token        = 0 '<unk>'
llm_load_print_meta: LF token         = 13 '<0x0A>'
llm_load_tensors: ggml ctx size       =    0.11 MiB
llm_load_tensors: using CUDA for GPU acceleration
llm_load_tensors: system memory used  =   70.43 MiB
llm_load_tensors: VRAM used           = 4095.06 MiB
llm_load_tensors: offloading 32 repeating layers to GPU
llm_load_tensors: offloading non-repeating layers to GPU
llm_load_tensors: offloaded 33/33 layers to GPU
...............................................................................................
llama_new_context_with_model: n_ctx      = 4096
llama_new_context_with_model: freq_base  = 10000.0
llama_new_context_with_model: freq_scale = 1
llama_new_context_with_model: KV self size  =  512.00 MiB, K (f16):  256.00 MiB, V (f16):  256.00 MiB
llama_build_graph: non-view tensors processed: 676/676
llama_new_context_with_model: compute buffer total size = 291.19 MiB
llama_new_context_with_model: VRAM scratch buffer: 288.00 MiB
llama_new_context_with_model: total VRAM used: 4383.07 MiB (model: 4095.06 MiB, context: 288.00 MiB)
AVX = 1 | AVX2 = 1 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | FMA = 1 | NEON = 0 | ARM_FMA = 0 | F16C = 1 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 1 | SSSE3 = 0 | VSX = 0 |
INFO:     Started server process [29248]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8016 (Press CTRL+C to quit)

llama_print_timings:        load time =     589.55 ms
llama_print_timings:      sample time =      26.09 ms /   188 runs   (    0.14 ms per token,  7206.10 tokens per second)
llama_print_timings: prompt eval time =     589.47 ms /    51 tokens (   11.56 ms per token,    86.52 tokens per second)
llama_print_timings:        eval time =    5116.05 ms /   187 runs   (   27.36 ms per token,    36.55 tokens per second)
llama_print_timings:       total time =    6124.55 ms
INFO:     *.*.*.*:3220 - "POST /v1/chat/completions HTTP/1.1" 200 OK

hpxiong avatar Apr 01 '24 23:04 hpxiong

That's very strange, same issue if you use the exact ip address ie base_url="http://0.0.0.0:8011/v1"?

abetlen avatar Apr 03 '24 05:04 abetlen

That's very strange, same issue if you use the exact ip address ie base_url="http://0.0.0.0:8011/v1"?

Yes, you are correct. I tried the call from the same machine as the llama_cpp_python server, all getting the same results.

hpxiong avatar Apr 04 '24 06:04 hpxiong

Has this been fixed? I'm getting to the same point.

niuhaoxing avatar Jul 12 '24 06:07 niuhaoxing

me either, I faced error message "Connect error" or "request timed out"

Foreist avatar Aug 26 '24 07:08 Foreist

Has this been fixed? I'm getting to the same point.

PotatoMine236 avatar Mar 12 '25 00:03 PotatoMine236

This isnt fixed yet, i get it when installing. Im working on a workaround for that, but is weird that it can find the server without the /v1 but i cant save, and if i put the /v1 i cant save neither, iwas using it on a vm, so installed the webui, but i think im gonna need to see the code and do it myself when i have time.

JoaoZaokk avatar May 04 '26 15:05 JoaoZaokk

Has this been resolved? I compiled llama.cpp today and this still does not work.

virtualJonesie avatar Jun 29 '26 19:06 virtualJonesie