Skip to content

Commit

Permalink
Add /v1/internal/logits endpoint (oobabooga#4650)
Browse files Browse the repository at this point in the history
  • Loading branch information
oobabooga authored Nov 19, 2023
1 parent 8f4f4da commit 0fa1af2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
23 changes: 23 additions & 0 deletions docs/12 - OpenAI API.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ curl http://127.0.0.1:5000/v1/chat/completions \
}'
```

#### Logits

```
curl -k http://127.0.0.1:5000/v1/internal/logits \
-H "Content-Type: application/json" \
-d '{
"prompt": "Who is best, Asuka or Rei? Answer:",
"use_samplers": false
}'
```

#### Logits after sampling parameters

```
curl -k http://127.0.0.1:5000/v1/internal/logits \
-H "Content-Type: application/json" \
-d '{
"prompt": "Who is best, Asuka or Rei? Answer:",
"use_samplers": true,
"top_k": 3
}'
```

#### Python chat example

```python
Expand Down
13 changes: 13 additions & 0 deletions extensions/openai/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import extensions.openai.completions as OAIcompletions
import extensions.openai.embeddings as OAIembeddings
import extensions.openai.images as OAIimages
import extensions.openai.logits as OAIlogits
import extensions.openai.models as OAImodels
import extensions.openai.moderations as OAImoderations
from extensions.openai.errors import ServiceUnavailableError
Expand All @@ -38,6 +39,8 @@
EncodeRequest,
EncodeResponse,
LoadModelRequest,
LogitsRequest,
LogitsResponse,
ModelInfoResponse,
TokenCountResponse,
to_dict
Expand Down Expand Up @@ -242,6 +245,16 @@ async def handle_token_count(request_data: EncodeRequest):
return JSONResponse(response)


@app.post("/v1/internal/logits", response_model=LogitsResponse, dependencies=check_key)
async def handle_logits(request_data: LogitsRequest):
'''
Given a prompt, returns the top 50 most likely logits as a dict.
The keys are the tokens, and the values are the probabilities.
'''
response = OAIlogits._get_next_logits(to_dict(request_data))
return JSONResponse(response)


@app.post("/v1/internal/stop-generation", dependencies=check_key)
async def handle_stop_generation(request: Request):
stop_everything_event()
Expand Down
24 changes: 21 additions & 3 deletions extensions/openai/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ class EncodeRequest(BaseModel):
text: str


class DecodeRequest(BaseModel):
class EncodeResponse(BaseModel):
tokens: List[int]
length: int


class EncodeResponse(BaseModel):
class DecodeRequest(BaseModel):
tokens: List[int]
length: int


class DecodeResponse(BaseModel):
Expand All @@ -143,6 +143,24 @@ class TokenCountResponse(BaseModel):
length: int


class LogitsRequestParams(BaseModel):
prompt: str
use_samplers: bool = False
frequency_penalty: float | None = 0
max_tokens: int | None = 16
presence_penalty: float | None = 0
temperature: float | None = 1
top_p: float | None = 1


class LogitsRequest(GenerationOptions, LogitsRequestParams):
pass


class LogitsResponse(BaseModel):
logits: dict


class ModelInfoResponse(BaseModel):
model_name: str
lora_names: List[str]
Expand Down
18 changes: 13 additions & 5 deletions modules/logits.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
global_scores = None


def get_next_logits(prompt, state, use_samplers, previous):
def get_next_logits(prompt, state, use_samplers, previous, return_dict=False):
if shared.model is None:
logger.error("No model is loaded! Select one in the Model tab.")
return 'Error: No model is loaded1 Select one in the Model tab.', previous
Expand Down Expand Up @@ -56,8 +56,16 @@ def get_next_logits(prompt, state, use_samplers, previous):
topk_indices = [i.expand((1, 1)) for i in topk_indices]

tokens = [shared.tokenizer.decode(i) for i in topk_indices]
output = ''
for row in list(zip(topk_values, tokens)):
output += f"{row[0]} - {repr(row[1])}\n"

return output, previous
if return_dict:
output = {}
for row in list(zip(topk_values, tokens)):
output[row[1]] = row[0]

return output
else:
output = ''
for row in list(zip(topk_values, tokens)):
output += f"{row[0]} - {repr(row[1])}\n"

return output, previous

0 comments on commit 0fa1af2

Please sign in to comment.