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

feat: implement support for limit, order, before, and after parameters in get_assistants #7537

Merged
merged 2 commits into from
Jan 3, 2025

Conversation

jeansouzak
Copy link
Contributor

Title

Implement support for limit, order, before, and after parameters in get_assistants
https://platform.openai.com/docs/api-reference/assistants/listAssistants

Relevant issues

Fixes #6530

Type

🆕 New Feature

Changes

  • Added support for limit and order parameters to allow control over the number of results and sorting direction (asc or desc).
  • Added support for before and after parameters to enable cursor-based pagination.
  • Ensured the API request now respects these parameters instead of returning a default of 20 items with no ordering.

[REQUIRED] Testing - Attach a screenshot of any new tests passing locally

Test case code:

import requests

def fetch_assistants(order=None, limit=None, before=None, after=None):
    url = "http://0.0.0.0:4000/v1/assistants"
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer jean@123"
    }
    
    # Add only non-None parameters to the request
    params = {
        "order": order,
        "limit": limit,
        "before": before,
        "after": after,
    }
    params = {k: v for k, v in params.items() if v is not None}  # Remove None values
    
    print(f"Testing with parameters: {params}")
    
    try:
        response = requests.get(url, headers=headers, params=params)
        response.raise_for_status()  # Raise an exception for HTTP errors
        data = response.json()
        
        if "data" in data:
            assistants = data["data"]
            print(f"Returned count: {len(assistants)}")
            
            for assistant in assistants:
                name = assistant.get("name", "No Name")
                id_ = assistant.get("id", "No ID")
                print(f"Name: {name}, ID: {id_}")
        else:
            print("Unexpected API response:", data)
    
    except requests.exceptions.RequestException as e:
        print("Error while making the request:", e)

if __name__ == "__main__":
    test_cases = [
        {},  # Test case with no parameters
        {"order": "asc", "limit": 2, "before": None, "after": None},
        {"order": "desc", "limit": 5, "before": None, "after": None},
        {"order": "asc", "limit": 4, "before": "asst_6PDftNbL9gprW8L14alVevU0", "after": None},  # Go Tutor
        {"order": "desc", "limit": 10, "before": None, "after": "asst_WCpsnvu6HQqxD1UalzC40aUG"},  # SQL Tutor
        {"order": "asc", "limit": 2, "before": "asst_eKTpuDe5OOxejJam1CD4sJzr", "after": None},  # Biology Tutor
        {"order": "desc", "limit": 1, "before": None, "after": "asst_DRs7oh2tYEcmcr4SG9tHqkys"},  # Game Code Tutor
    ]

    for case in test_cases:
        fetch_assistants(**case)
        print("-" * 50)

Results:

myenv) jeansouza@Jeans-Laptop test-litellm % python3 curl-assistant-api.py
Testing with parameters: {}
Returned count: 20
Name: Git Tutor, ID: asst_WAW8Cjt331mqTRwFT48aMbzr
Name: C Tutor, ID: asst_hTePoqSkgPTN1JUZj2o3hJzZ
Name: Go Tutor, ID: asst_6PDftNbL9gprW8L14alVevU0
Name: Java Tutor, ID: asst_dysBers4SBpp9T7lljIuaulH
Name: SQL Tutor, ID: asst_WCpsnvu6HQqxD1UalzC40aUG
Name: C# Tutor, ID: asst_N2IeW4pFamLHwSJriHyhDst3
Name: Angular Tutor, ID: asst_HK26MolragMP8x5sksSYdgyK
Name: VueJS Tutor, ID: asst_IAcjTnbBFotMbXFO9rp0EEba
Name: JavaScript Tutor, ID: asst_f5dCwR2zvctcfTAjVmyLV6Xe
Name: PHP Tutor, ID: asst_2xrvm7jGTWeOchbW8StMRcxt
Name: Lua Tutor, ID: asst_okxVum8b4z6LGGPURsHTHgoV
Name: C++ Tutor, ID: asst_vnpwQtcAROXB7cewGPoagcjw
Name: Game code Tutor, ID: asst_DRs7oh2tYEcmcr4SG9tHqkys
Name: Sociology Tutor, ID: asst_1HqXedKRd6dHJlYXjIPQeJwb
Name: Psicology Tutor, ID: asst_38UQ7Q4NKJfbrN9UDDkJ7Cnx
Name: Biology Tutor, ID: asst_eKTpuDe5OOxejJam1CD4sJzr
Name: Computer Science Tutor, ID: asst_K4MIvzlZgHUvwLwpNcqJTil3
Name: English Tutor, ID: asst_VBMZE4TZ41whZGEDuoNYDGeA
Name: Polish Tutor, ID: asst_BD9LdbZQWCOiKtL4irS11IJm
Name: Germany Tutor, ID: asst_jpb3YhQuFZovPUSIfHItTDCD
--------------------------------------------------
Testing with parameters: {'order': 'asc', 'limit': 2}
Returned count: 2
Name: Math Tutor, ID: asst_XjFYvqpVFyAsdWmtdIkygdAJ
Name: Portuguese Tutor, ID: asst_WdYY4vi1v62Wra6zEX9aBXBX
--------------------------------------------------
Testing with parameters: {'order': 'desc', 'limit': 5}
Returned count: 5
Name: Git Tutor, ID: asst_WAW8Cjt331mqTRwFT48aMbzr
Name: C Tutor, ID: asst_hTePoqSkgPTN1JUZj2o3hJzZ
Name: Go Tutor, ID: asst_6PDftNbL9gprW8L14alVevU0
Name: Java Tutor, ID: asst_dysBers4SBpp9T7lljIuaulH
Name: SQL Tutor, ID: asst_WCpsnvu6HQqxD1UalzC40aUG
--------------------------------------------------
Testing with parameters: {'order': 'asc', 'limit': 4, 'before': 'asst_6PDftNbL9gprW8L14alVevU0'}
Returned count: 4
Name: Math Tutor, ID: asst_XjFYvqpVFyAsdWmtdIkygdAJ
Name: Portuguese Tutor, ID: asst_WdYY4vi1v62Wra6zEX9aBXBX
Name: Germany Tutor, ID: asst_jpb3YhQuFZovPUSIfHItTDCD
Name: Polish Tutor, ID: asst_BD9LdbZQWCOiKtL4irS11IJm
--------------------------------------------------
Testing with parameters: {'order': 'desc', 'limit': 10, 'after': 'asst_WCpsnvu6HQqxD1UalzC40aUG'}
Returned count: 10
Name: C# Tutor, ID: asst_N2IeW4pFamLHwSJriHyhDst3
Name: Angular Tutor, ID: asst_HK26MolragMP8x5sksSYdgyK
Name: VueJS Tutor, ID: asst_IAcjTnbBFotMbXFO9rp0EEba
Name: JavaScript Tutor, ID: asst_f5dCwR2zvctcfTAjVmyLV6Xe
Name: PHP Tutor, ID: asst_2xrvm7jGTWeOchbW8StMRcxt
Name: Lua Tutor, ID: asst_okxVum8b4z6LGGPURsHTHgoV
Name: C++ Tutor, ID: asst_vnpwQtcAROXB7cewGPoagcjw
Name: Game code Tutor, ID: asst_DRs7oh2tYEcmcr4SG9tHqkys
Name: Sociology Tutor, ID: asst_1HqXedKRd6dHJlYXjIPQeJwb
Name: Psicology Tutor, ID: asst_38UQ7Q4NKJfbrN9UDDkJ7Cnx
--------------------------------------------------
Testing with parameters: {'order': 'asc', 'limit': 2, 'before': 'asst_eKTpuDe5OOxejJam1CD4sJzr'}
Returned count: 2
Name: Math Tutor, ID: asst_XjFYvqpVFyAsdWmtdIkygdAJ
Name: Portuguese Tutor, ID: asst_WdYY4vi1v62Wra6zEX9aBXBX
--------------------------------------------------
Testing with parameters: {'order': 'desc', 'limit': 1, 'after': 'asst_DRs7oh2tYEcmcr4SG9tHqkys'}
Returned count: 1
Name: Sociology Tutor, ID: asst_1HqXedKRd6dHJlYXjIPQeJwb
--------------------------------------------------
  1. Limit and Order Functionality:
  • Perform an API request with limit=10 and verify that exactly 10 results are returned.
  • Perform an API request with order=asc and desc and validate that the results are sorted correctly by creation timestamp.
  1. Pagination Parameters (before, after)
  • Perform an API request with after=obj_foo and ensure that the results start after obj_foo.
  • Perform an API request with before=obj_bar and ensure that the results end before obj_bar.
  1. Default Behavior:
  • Perform an API request without limit, order, before, and after and ensure it defaults to 20 items with descending order.

…sistants method for openai

- Ensured that `before` and `after` parameters are only passed when provided to avoid AttributeError.
- Implemented safe access using default values for `before` and `after` to prevent missing attribute issues.
- Added consistent handling of `order` and `limit` to improve flexibility and robustness in API calls.
Copy link

vercel bot commented Jan 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 3, 2025 9:23pm

@krrishdholakia krrishdholakia merged commit 4b0505d into BerriAI:main Jan 3, 2025
2 checks passed
rajatvig pushed a commit to rajatvig/litellm that referenced this pull request Jan 16, 2025
…sistants method for openai (BerriAI#7537)

- Ensured that `before` and `after` parameters are only passed when provided to avoid AttributeError.
- Implemented safe access using default values for `before` and `after` to prevent missing attribute issues.
- Added consistent handling of `order` and `limit` to improve flexibility and robustness in API calls.
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.

[Bug]: Assistant Listing Limited to 20 with Duplications – Limit Parameter Ignored
2 participants