Skip to content
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

compatible with oobabooga text-generation-web-ui #1019

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cycloarcane
Copy link

@cycloarcane cycloarcane commented Nov 21, 2024

This change adds a modified rest.py that is compatible with oobabooga text generation webui as oobabooga uses nested JSON in its API responses. I added the _get_nested_value method to the rest.py template file.

An example of the nested JSON response from oobabooga:

curl -X POST http://127.0.0.1:5000/v1/completions -H "Content-Type: application/json" -d '{"prompt": "Hello"}'

{"id":"conv-1732215852476260864","object":"text_completion","created":1732215852,"model":"WhiteRabbitNeo_Llama-3.1-WhiteRabbitNeo-2-8B","choices":[{"index":0,"finish_reason":"length","text":" and Happy New Year to all who cruised past this channel during the year gone","logprobs":{"top_logprobs":[{}]}}],"usage":{"prompt_tokens":2,"completion_tokens":17,"total_tokens":19}}%   

Verification

  • Supporting configuration such as generator configuration file
  "rest": {
    "RestGenerator": {
      "name": "oobabooga Local Model",
      "uri": "http://127.0.0.1:5000/v1/completions",
      "method": "post",
      "headers": {
        "Content-Type": "application/json"
      },
      "req_template_json_object": {
        "prompt": "$INPUT",
        "max_tokens": 1000,
        "temperature": 0.7
      },
      "response_json": true,
      "response_json_field": "choices[0].text"
    }
  }
}

I have signed off the commit. Here is the method that was added:

def _get_nested_value(self, obj, path):
       """Helper method to get nested dictionary values using a path string."""
       try:
           current = obj
           parts = path.replace("][", "].").replace("[", ".").replace("]", "").split(".")
           for part in parts:
               if part.isdigit():
                   current = current[int(part)]
               else:
                   current = current[part]
           return current
       except (KeyError, IndexError, TypeError) as e:
           logging.error(f"Failed to access path '{path}' in object: {e}")
           return None

I have tested and run garak successfully with oobabooga text generation webui after making these changes.

Copy link
Contributor

github-actions bot commented Nov 21, 2024

DCO Assistant Lite bot All contributors have signed the DCO ✍️ ✅

@cycloarcane
Copy link
Author

I have read the DCO Document and I hereby sign the DCO

@cycloarcane
Copy link
Author

recheck

github-actions bot added a commit that referenced this pull request Nov 21, 2024
@jmartin-tech
Copy link
Collaborator

The existing restGenerator supports extracting the response via JSONPath provided in the response_json_field as of #651, what new functionality is added here?

Please test using a JSONPath as response_json_field in your example with the existing generator, something like:

      "response_json_field": "$.choices[0].text"

Also please note that unless there is significant change need a new module is not needed. The proposed change here introduces significant code duplication that can easily be avoided.

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