|
| 1 | +--- |
| 2 | +title: Chat Completion |
| 3 | +--- |
| 4 | + |
| 5 | +The Chat Completion feature in Nitro provides a flexible way to interact with any local Large Language Model (LLM). |
| 6 | + |
| 7 | +## Single Request Example |
| 8 | + |
| 9 | +To send a single query to your chosen LLM, follow these steps: |
| 10 | + |
| 11 | +<div style={{ width: '50%', float: 'left', clear: 'left' }}> |
| 12 | + |
| 13 | +```bash title="Nitro" |
| 14 | +curl http://localhost:3928/inferences/llamacpp/chat_completion \ |
| 15 | + -H "Content-Type: application/json" \ |
| 16 | + -d '{ |
| 17 | + "model": "", |
| 18 | + "messages": [ |
| 19 | + { |
| 20 | + "role": "user", |
| 21 | + "content": "Hello" |
| 22 | + }, |
| 23 | + ] |
| 24 | + }' |
| 25 | + |
| 26 | +``` |
| 27 | +</div> |
| 28 | + |
| 29 | +<div style={{ width: '50%', float: 'right', clear: 'right' }}> |
| 30 | + |
| 31 | +```bash title="OpenAI" |
| 32 | +curl https://api.openai.com/v1/chat/completions \ |
| 33 | + -H "Content-Type: application/json" \ |
| 34 | + -H "Authorization: Bearer $OPENAI_API_KEY" \ |
| 35 | + -d '{ |
| 36 | + "model": "gpt-3.5-turbo", |
| 37 | + "messages": [ |
| 38 | + { |
| 39 | + "role": "user", |
| 40 | + "content": "Hello" |
| 41 | + } |
| 42 | + ] |
| 43 | + }' |
| 44 | +``` |
| 45 | +</div> |
| 46 | + |
| 47 | +This command sends a request to your local LLM, querying about the winner of the 2020 World Series. |
| 48 | + |
| 49 | +### Dialog Request Example |
| 50 | + |
| 51 | +For ongoing conversations or multiple queries, the dialog request feature is ideal. Here’s how to structure a multi-turn conversation: |
| 52 | + |
| 53 | +<div style={{ width: '50%', float: 'left', clear: 'left' }}> |
| 54 | + |
| 55 | +```bash title="Nitro" |
| 56 | +curl http://localhost:3928/inferences/llamacpp/chat_completion \ |
| 57 | + -H "Content-Type: application/json" \ |
| 58 | + -d '{ |
| 59 | + "messages": [ |
| 60 | + { |
| 61 | + "role": "system", |
| 62 | + "content": "You are a helpful assistant." |
| 63 | + }, |
| 64 | + { |
| 65 | + "role": "user", |
| 66 | + "content": "Who won the world series in 2020?" |
| 67 | + }, |
| 68 | + { |
| 69 | + "role": "assistant", |
| 70 | + "content": "The Los Angeles Dodgers won the World Series in 2020." |
| 71 | + }, |
| 72 | + { |
| 73 | + "role": "user", |
| 74 | + "content": "Where was it played?" |
| 75 | + } |
| 76 | + ] |
| 77 | + }' |
| 78 | + |
| 79 | +``` |
| 80 | +</div> |
| 81 | + |
| 82 | +<div style={{ width: '50%', float: 'right', clear: 'right' }}> |
| 83 | + |
| 84 | +```bash title="OpenAI" |
| 85 | +curl https://api.openai.com/v1/chat/completions \ |
| 86 | + -H "Content-Type: application/json" \ |
| 87 | + -H "Authorization: Bearer $OPENAI_API_KEY" \ |
| 88 | + -d '{ |
| 89 | + "messages": [ |
| 90 | + { |
| 91 | + "role": "system", |
| 92 | + "content": "You are a helpful assistant." |
| 93 | + }, |
| 94 | + { |
| 95 | + "role": "user", |
| 96 | + "content": "Who won the world series in 2020?" |
| 97 | + }, |
| 98 | + { |
| 99 | + "role": "assistant", |
| 100 | + "content": "The Los Angeles Dodgers won the World Series in 2020." |
| 101 | + }, |
| 102 | + { |
| 103 | + "role": "user", |
| 104 | + "content": "Where was it played?" |
| 105 | + } |
| 106 | + ] |
| 107 | + }' |
| 108 | +``` |
| 109 | +</div> |
| 110 | + |
| 111 | +### Chat Completion Response |
| 112 | + |
| 113 | +Below are examples of responses from both the Nitro server and OpenAI: |
| 114 | + |
| 115 | +<div style={{ width: '50%', float: 'left', clear: 'left' }}> |
| 116 | + |
| 117 | +```js title="Nitro" |
| 118 | +{ |
| 119 | + "choices": [ |
| 120 | + { |
| 121 | + "finish_reason": null, |
| 122 | + "index": 0, |
| 123 | + "message": { |
| 124 | + "content": "Hello, how may I assist you this evening?", |
| 125 | + "role": "assistant" |
| 126 | + } |
| 127 | + } |
| 128 | + ], |
| 129 | + "created": 1700215278, |
| 130 | + "id": "sofpJrnBGUnchO8QhA0s", |
| 131 | + "model": "_", |
| 132 | + "object": "chat.completion", |
| 133 | + "system_fingerprint": "_", |
| 134 | + "usage": { |
| 135 | + "completion_tokens": 13, |
| 136 | + "prompt_tokens": 90, |
| 137 | + "total_tokens": 103 |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | +</div> |
| 142 | + |
| 143 | +<div style={{ width: '50%', float: 'right', clear: 'right' }}> |
| 144 | + |
| 145 | +```js title="OpenAI" |
| 146 | +{ |
| 147 | + "choices": [ |
| 148 | + { |
| 149 | + "finish_reason": "stop" |
| 150 | + "index": 0, |
| 151 | + "message": { |
| 152 | + "role": "assistant", |
| 153 | + "content": "Hello there, how may I assist you today?", |
| 154 | + } |
| 155 | + } |
| 156 | + ], |
| 157 | + "created": 1677652288, |
| 158 | + "id": "chatcmpl-123", |
| 159 | + "model": "gpt-3.5-turbo-0613", |
| 160 | + "object": "chat.completion", |
| 161 | + "system_fingerprint": "fp_44709d6fcb", |
| 162 | + "usage": { |
| 163 | + "completion_tokens": 12, |
| 164 | + "prompt_tokens": 9, |
| 165 | + "total_tokens": 21 |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | +</div> |
| 170 | + |
| 171 | + |
| 172 | +The chat completion feature in Nitro showcases compatibility with OpenAI, making the transition between using OpenAI and local AI models more straightforward. For further details and advanced usage, please refer to the [API reference](https://nitro.jan.ai/api). |
0 commit comments