Skip to content

feat: configurable examples #132

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

freinold
Copy link
Owner

@freinold freinold commented Jul 20, 2025

Closes #122

Summary by CodeRabbit

  • New Features

    • Centralized and expanded example data for API documentation and frontend, with dynamic loading and structured validation.
    • Introduced a standardized entity data structure for consistent representation of extracted entities.
  • Improvements

    • Example values in API requests, responses, and frontend UI are now dynamically sourced from a centralized example set, ensuring consistency and easier updates.
    • Input constraints improved for batch requests.
  • Removals

    • Removed hardcoded example values and the helper function for merging overlapping entities.

@freinold freinold added this to the v0.3 milestone Jul 20, 2025
@freinold freinold self-assigned this Jul 20, 2025
@freinold freinold added the enhancement New feature or request label Jul 20, 2025
Copy link
Contributor

coderabbitai bot commented Jul 20, 2025

Walkthrough

A centralized examples system was introduced using new Pydantic models and a configuration-backed loader, enabling dynamic example data for both frontend and API documentation. The Entity data model was refactored and moved, imports were updated accordingly, and hardcoded example data was replaced with dynamic sources. A legacy helper function was removed.

Changes

File(s) Change Summary
gliner_api/__init__.py Added new Entity Pydantic model with fields for entity extraction results.
gliner_api/datamodel.py Removed local Entity class, imported from gliner_api; switched all example data to use new dynamic Examples class; added min_length constraint to batch texts; updated TypeAdapter usage.
gliner_api/examples.py Introduced new module with Examples, InvokeExample, BatchExample classes, and a function to load and cache example data from config files.
gliner_api/frontend.py Updated to use dynamic examples for UI defaults and example sets, replacing hardcoded literals.
gliner_api/backend.py Updated import of Entity to new location in gliner_api.
gliner_api/helpers.py Deleted file; removed the merge_overlapping_entities function.

Sequence Diagram(s)

sequenceDiagram
    participant Frontend
    participant Examples
    participant API_Docs

    Frontend->>Examples: Load example data (get_examples)
    API_Docs->>Examples: Load example data (get_examples)
    Examples-->>Frontend: Provide example inputs/outputs
    Examples-->>API_Docs: Provide example inputs/outputs
Loading

Suggested labels

documentation

Poem

A bunny with code on its mind,
Hopped to make examples aligned.
From hardcoded to dynamic,
The docs look less frantic—
Now frontend and docs are entwined!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0801d1a and adc5dfc.

📒 Files selected for processing (1)
  • gliner_api/frontend.py (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • gliner_api/frontend.py
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
gliner_api/frontend.py (1)

127-131: Consider simplifying the complex boolean mapping logic.

The current logic for mapping boolean flags to option strings is complex and hard to read. Consider extracting this into a helper function.

+def get_checkbox_options(flat_ner: bool, multi_label: bool) -> list[str]:
+    """Convert boolean flags to checkbox option strings."""
+    options = []
+    if not flat_ner:
+        options.append("deep_ner")
+    if multi_label:
+        options.append("multi_label")
+    return options
+
         gr.CheckboxGroup(
             label="Additional Options (Advanced)",
             choices=[
                 ("Enable deep NER mode", "deep_ner"),
                 ("Enable multi-label classification", "multi_label"),
             ],
-            value=[
-                value
-                for option, value in zip([not examples.invoke[0].flat_ner, examples.invoke[0].multi_label], ["deep_ner", "multi_label"])
-                if option
-            ],
+            value=get_checkbox_options(examples.invoke[0].flat_ner, examples.invoke[0].multi_label),
gliner_api/examples.py (1)

15-15: Consider potential circular dependency issue.

The examples module imports get_config() at module level, while config.py might reference examples in validation. This could create circular import issues.

Consider lazy loading the config:

-config: Config = get_config()

 class InvokeExample(BaseSettings):
     text: str
-    threshold: float = Field(ge=0.0, le=1.0, default=config.default_threshold)
-    entity_types: list[str] = Field(default=config.default_entities)
+    threshold: float = Field(ge=0.0, le=1.0, default_factory=lambda: get_config().default_threshold)
+    entity_types: list[str] = Field(default_factory=lambda: get_config().default_entities)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 97b2f28 and 0b235d9.

📒 Files selected for processing (6)
  • gliner_api/__init__.py (1 hunks)
  • gliner_api/backend.py (1 hunks)
  • gliner_api/datamodel.py (2 hunks)
  • gliner_api/examples.py (1 hunks)
  • gliner_api/frontend.py (4 hunks)
  • gliner_api/helpers.py (0 hunks)
💤 Files with no reviewable changes (1)
  • gliner_api/helpers.py
🧰 Additional context used
🧠 Learnings (2)
gliner_api/backend.py (1)
Learnt from: freinold
PR: freinold/GLiNER-API#86
File: gliner_api/frontend.py:13-13
Timestamp: 2025-07-07T10:44:27.531Z
Learning: In the GLiNER-API project, the frontend and backend are always run on the same machine, with the frontend bound to the same FastAPI backend in main.py. This architectural constraint makes hardcoded localhost connections appropriate and eliminates the need for host configurability in the frontend client.
gliner_api/frontend.py (1)
Learnt from: freinold
PR: freinold/GLiNER-API#86
File: gliner_api/frontend.py:13-13
Timestamp: 2025-07-07T10:44:27.531Z
Learning: In the GLiNER-API project, the frontend and backend are always run on the same machine, with the frontend bound to the same FastAPI backend in main.py. This architectural constraint makes hardcoded localhost connections appropriate and eliminates the need for host configurability in the frontend client.
🧬 Code Graph Analysis (3)
gliner_api/backend.py (1)
gliner_api/__init__.py (1)
  • Entity (4-24)
gliner_api/frontend.py (3)
gliner_api/examples.py (2)
  • Examples (18-83)
  • get_examples (105-107)
gliner_api/config.py (2)
  • Config (8-110)
  • get_config (114-116)
gliner_api/backend.py (2)
  • invoke (115-155)
  • info (215-225)
gliner_api/datamodel.py (3)
gliner_api/__init__.py (1)
  • Entity (4-24)
gliner_api/config.py (2)
  • Config (8-110)
  • get_config (114-116)
gliner_api/examples.py (2)
  • Examples (18-83)
  • get_examples (105-107)
🔇 Additional comments (7)
gliner_api/__init__.py (1)

4-24: LGTM! Well-structured Entity model with proper validation.

The Entity model is well-designed with appropriate field constraints, clear descriptions, and flexible type/label aliasing. Moving it to __init__.py makes it easily accessible across the codebase.

gliner_api/backend.py (1)

13-13: LGTM! Import correctly updated for Entity model relocation.

The import statement properly reflects the new location of the Entity model in gliner_api/__init__.py.

gliner_api/frontend.py (1)

10-13: LGTM! Good integration with the new examples system.

The frontend properly integrates with the centralized examples system, making the UI dynamic and configurable.

gliner_api/examples.py (1)

1-16: LGTM! Well-structured examples module with proper configuration support.

The module provides a clean way to centralize and configure example data. The use of BaseSettings with file support is excellent for maintainability.

gliner_api/datamodel.py (3)

1-8: LGTM! Clean refactoring to use centralized examples system.

The import updates and dynamic examples usage properly integrate with the new examples module, eliminating hardcoded example data.


56-56: LGTM! Good addition of validation constraint.

Adding min_length=1 to BatchRequest.texts ensures that batch requests contain at least one text, which is a sensible validation.


126-127: LGTM! Proper TypeAdapter syntax with keyword arguments.

The update to use type= keyword arguments in TypeAdapter instantiation follows best practices and improves code clarity.

Comment on lines +24 to +28
Entity(start=0, end=10, text="Steve Jobs", type="person", score=0.99),
Entity(start=19, end=24, text="Apple", type="organization", score=0.98),
Entity(start=28, end=37, text="Cupertino", type="location", score=0.98),
Entity(start=39, end=49, text="California", type="location", score=0.99),
Entity(start=53, end=66, text="April 1, 1976", type="date", score=0.68),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify entity start/end indices are accurate.

Please double-check that the entity boundaries are correct. For example:

  • "Steve Jobs" (0-10): ✓ Correct
  • "Apple" (19-24): ✓ Correct
  • "Cupertino" (28-37): ✓ Correct

🏁 Script executed:

#!/bin/bash
# Verify entity indices in example text
text="Steve Jobs founded Apple Inc. in Cupertino, CA on April 1, 1976."
echo "Text: $text"
echo "Length: ${#text}"
echo ""
echo "Entity extractions:"
echo "0-10: '${text:0:10}'"
echo "19-24: '${text:19:5}'"
echo "28-37: '${text:28:9}'"
echo "39-49: '${text:39:10}'"
echo "53-66: '${text:53:13}'"

Length of output: 578


Update entity indices to match the actual example text

After verifying the string

Steve Jobs founded Apple Inc. in Cupertino, CA on April 1, 1976.

the spans for “Cupertino”, “CA”/“California” and the date are off. Please correct lines 24–28 in gliner_api/examples.py:

  • Cupertino is at indices 33–42, not 28–37
  • The text contains “CA” at 44–46 (or change the example to “California” if you want that full name)
  • “April 1, 1976” spans 50–63, not 53–66
- Entity(start=28, end=37, text="Cupertino", type="location", score=0.98),
- Entity(start=39, end=49, text="California", type="location", score=0.99),
- Entity(start=53, end=66, text="April 1, 1976", type="date", score=0.68),
+ Entity(start=33, end=42, text="Cupertino", type="location", score=0.98),
+ Entity(start=44, end=46, text="CA",         type="location", score=0.99),
+ Entity(start=50, end=63, text="April 1, 1976", type="date",       score=0.68),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Entity(start=0, end=10, text="Steve Jobs", type="person", score=0.99),
Entity(start=19, end=24, text="Apple", type="organization", score=0.98),
Entity(start=28, end=37, text="Cupertino", type="location", score=0.98),
Entity(start=39, end=49, text="California", type="location", score=0.99),
Entity(start=53, end=66, text="April 1, 1976", type="date", score=0.68),
Entity(start=0, end=10, text="Steve Jobs", type="person", score=0.99),
Entity(start=19, end=24, text="Apple", type="organization", score=0.98),
Entity(start=33, end=42, text="Cupertino", type="location", score=0.98),
Entity(start=44, end=46, text="CA", type="location", score=0.99),
Entity(start=50, end=63, text="April 1, 1976", type="date", score=0.68),
🤖 Prompt for AI Agents
In gliner_api/examples.py around lines 24 to 28, the entity span indices for
"Cupertino", "CA"/"California", and "April 1, 1976" do not match the actual
example text. Update the start and end indices for "Cupertino" to 33–42, add or
adjust the entity for "CA" at 44–46 (or replace with "California" if preferred),
and correct the date span to 50–63 to align with the string "Steve Jobs founded
Apple Inc. in Cupertino, CA on April 1, 1976."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make frontend and API doc examples configurable
1 participant