Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for logit_bias and logit_bias_type parameters #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2023

Conversation

player1537
Copy link
Contributor

I was inspired by the way simpleaichat works and I wanted to try running it on my own hardware with my own API server. This code relies on the logit_bias parameter to work, so I needed to add it.

The way OpenAI's API works is that the logit_bias is a mapping from input_ids (as a string) to a bias (as a float). Because of the need to use IDs instead of just the token strings themselves, then it means this parameter and its functionality is tied to ChatGPT's tokenizer. This is a problem for other models, obviously.

So in addition to implementing the API as OpenAI intended, I also added another parameter that changes the meaning of logit_bias to be a mapping from token strings (as a string) to a bias (as a float).

I tested this with the following pair of requests:

$ curl --verbose --location -H 'Content-Type: application/json' http://localhost:7777/v1/completions --data @<(exec jq --null-input --arg prompt "USER: What is the result of 20+4? ASSISTANT:" 'reduce ( range(0;10) | { (.|tostring): 100.0 } ) as $x ({}; . + $x) | . as $logit_bias | { prompt: $prompt, logit_bias: $logit_bias, logit_bias_type: "tokens", max_tokens: 1 }')

$ curl --verbose --location -H 'Content-Type: application/json' http://localhost:7777/v1/chat/completions --data @<(exec jq --null-input --arg prompt "What is the result of 3+5?" 'reduce ( range(0;10) | { (.|tostring): 100.0 } ) as $x ({}; . + $x) | . as $logit_bias | { messages: [{ "role": "user", "content": $prompt }], logit_bias: $logit_bias, logit_bias_type: "tokens", max_tokens: 1 }')

The jq commands correspond to the following JSON objects.

{
  "prompt": "USER: What is the result of 20+4? ASSISTANT:",
  "logit_bias": {
    "0": 100,
    "1": 100,
    "2": 100,
    "3": 100,
    "4": 100,
    "5": 100,
    "6": 100,
    "7": 100,
    "8": 100,
    "9": 100
  },
  "logit_bias_type": "tokens",
  "max_tokens": 1
}

{
  "messages": [
    {
      "role": "user",
      "content": "What is the result of 3+5?"
    }
  ],
  "logit_bias": {
    "0": 100,
    "1": 100,
    "2": 100,
    "3": 100,
    "4": 100,
    "5": 100,
    "6": 100,
    "7": 100,
    "8": 100,
    "9": 100
  },
  "logit_bias_type": "tokens",
  "max_tokens": 1
}

@abetlen
Copy link
Owner

abetlen commented Jun 10, 2023

Very interesting, I'll test this out!

@abetlen abetlen merged commit f568bae into abetlen:main Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants