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

How to limit the token number? #7

Open
gnusupport opened this issue Feb 25, 2025 · 1 comment
Open

How to limit the token number? #7

gnusupport opened this issue Feb 25, 2025 · 1 comment

Comments

@gnusupport
Copy link

from fastapi import FastAPI
from pydantic import BaseModel
from semantic_split import SimilarSentenceSplitter, SentenceTransformersSimilarity, SpacySentenceSplitter

app = FastAPI()

class TextRequest(BaseModel):
    text: str
    max_tokens: int = 8192  # Default value for max_tokens

@app.post("/split")
async def split_text(request: TextRequest):
    # Initialize the splitter with the specified max_tokens
    model = SentenceTransformersSimilarity()
    sentence_splitter = SpacySentenceSplitter()
    splitter = SimilarSentenceSplitter(model, sentence_splitter, max_tokens=request.max_tokens)  # Pass max_tokens here

    # Perform the semantic split
    res = splitter.split(request.text)
    # Return the result as JSON
    return {"result": res}

# Run the server
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="192.168.1.68", port=8887)

I am running it as above, but I need way to limit the token number, is there any way?

@gnusupport
Copy link
Author

from fastapi import FastAPI
from pydantic import BaseModel
from semantic_split import SimilarSentenceSplitter, SentenceTransformersSimilarity, SpacySentenceSplitter

app = FastAPI()

class TextRequest(BaseModel):
    text: str
    max_sentences: int = 20  # Default value for max_sentences

@app.post("/split")
async def split_text(request: TextRequest):
    # Initialize the splitter with the specified max_tokens
    model = SentenceTransformersSimilarity()
    sentence_splitter = SpacySentenceSplitter()
    splitter = SimilarSentenceSplitter(model, sentence_splitter)  

    # Perform the semantic split
    res = splitter.split(request.text, group_max_sentences=request.max_sentences)  # Pass group_max_sentences here
    # Return the result as JSON
    return {"result": res}

# Run the server
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="192.168.1.68", port=8887)

I will try with this solution.

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

No branches or pull requests

1 participant