Skip to content

How to limit the token number? #7

@gnusupport

Description

@gnusupport
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions