|
| 1 | +LoRAX supports [OpenAI Chat Completions v1](https://platform.openai.com/docs/api-reference/completions/create) compatible endpoints that serve as a drop-in replacement for the OpenAI SDK. It supports multi-turn |
| 2 | +chat conversations while retaining support for dynamic adapter loading. |
| 3 | + |
| 4 | +## Chat Completions v1 |
| 5 | + |
| 6 | +Using the existing OpenAI Python SDK, replace the `base_url` with your LoRAX endpoint with `/v1` appended. The `api_key` can be anything, as it is unused. |
| 7 | + |
| 8 | +The `model` parameter can be set to the empty string `""` to use the base model, or any adapter ID on the HuggingFace hub. |
| 9 | + |
| 10 | +```python |
| 11 | +from openai import OpenAI |
| 12 | + |
| 13 | +client = OpenAI( |
| 14 | + api_key="EMPTY", |
| 15 | + base_url="http://127.0.0.1:8080/v1", |
| 16 | +) |
| 17 | + |
| 18 | +resp = client.chat.completions.create( |
| 19 | + model="alignment-handbook/zephyr-7b-dpo-lora", |
| 20 | + messages=[ |
| 21 | + { |
| 22 | + "role": "system", |
| 23 | + "content": "You are a friendly chatbot who always responds in the style of a pirate", |
| 24 | + }, |
| 25 | + {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, |
| 26 | + ], |
| 27 | + max_tokens=100, |
| 28 | +) |
| 29 | +print("Response:", resp[0].choices[0].text) |
| 30 | +``` |
| 31 | + |
| 32 | +### Streaming |
| 33 | + |
| 34 | +The streaming API is supported with the `stream=True` parameter: |
| 35 | + |
| 36 | +```python |
| 37 | +messages = client.chat.completions.create( |
| 38 | + model="alignment-handbook/zephyr-7b-dpo-lora", |
| 39 | + messages=[ |
| 40 | + { |
| 41 | + "role": "system", |
| 42 | + "content": "You are a friendly chatbot who always responds in the style of a pirate", |
| 43 | + }, |
| 44 | + {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, |
| 45 | + ], |
| 46 | + max_tokens=100, |
| 47 | + stream=True, |
| 48 | +) |
| 49 | + |
| 50 | +for message in messages: |
| 51 | + print(message) |
| 52 | +``` |
| 53 | + |
| 54 | +### REST API |
| 55 | + |
| 56 | +The REST API can be used directly in addition to the Python SDK: |
| 57 | + |
| 58 | +```bash |
| 59 | +curl http://127.0.0.1:8080/v1/chat/completions \ |
| 60 | +-H "Content-Type: application/json" \ |
| 61 | +-d '{ |
| 62 | + "model": "alignment-handbook/zephyr-7b-dpo-lora", |
| 63 | + "messages": [ |
| 64 | + { |
| 65 | + "role": "system", |
| 66 | + "content": "You are a friendly chatbot who always responds in the style of a pirate" |
| 67 | + }, |
| 68 | + { |
| 69 | + "role": "user", |
| 70 | + "content": "How many helicopters can a human eat in one sitting?" |
| 71 | + } |
| 72 | + ], |
| 73 | + "max_tokens": 100 |
| 74 | +}' |
| 75 | +``` |
| 76 | + |
| 77 | +### Chat Templates |
| 78 | + |
| 79 | +Multi-turn chat conversations are supported through [HuggingFace chat templates](https://huggingface.co/docs/transformers/chat_templating). |
| 80 | + |
| 81 | +If the adapter selected with the `model` parameter has its own tokenizer and chat template, LoRAX will apply the adapter's chat template |
| 82 | +to the request during inference. If, however, the adapter does not have its own chat template, LoRAX will fallback to using the base model |
| 83 | +chat template. If this does not exist, an error will be raised, as chat templates are required for multi-turn conversations. |
| 84 | + |
| 85 | +## (Legacy) Completions v1 |
| 86 | + |
| 87 | +The legacy completions v1 API can be used as well: |
| 88 | + |
| 89 | +```python |
| 90 | +from openai import OpenAI |
| 91 | + |
| 92 | +client = OpenAI( |
| 93 | + api_key="EMPTY", |
| 94 | + base_url="http://127.0.0.1:8080/v1", |
| 95 | +) |
| 96 | + |
| 97 | +# synchrounous completions |
| 98 | +completion = client.completions.create( |
| 99 | + model=adapter_id, |
| 100 | + prompt=prompt, |
| 101 | +) |
| 102 | +print("Completion result:", completion[0].choices[0].text) |
| 103 | + |
| 104 | +# streaming completions |
| 105 | +completion_stream = client.completions.create( |
| 106 | + model=adapter_id, |
| 107 | + prompt=prompt, |
| 108 | + stream=True, |
| 109 | +) |
| 110 | + |
| 111 | +for message in completion_stream: |
| 112 | + print("Completion message:", message) |
| 113 | +``` |
| 114 | + |
| 115 | +REST: |
| 116 | + |
| 117 | +```bash |
| 118 | +curl http://127.0.0.1:8080/v1/completions \ |
| 119 | +-H "Content-Type: application/json" \ |
| 120 | +-d '{ |
| 121 | +"model": "", |
| 122 | +"prompt": "Instruct: Write a detailed analogy between mathematics and a lighthouse.\nOutput:", |
| 123 | +"max_tokens": 100 |
| 124 | +}' |
| 125 | +``` |
0 commit comments