Truncate text for zhipu embedding. (#4490)

### What problem does this PR solve?


### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Kevin Hu 2025-01-15 14:36:27 +08:00 committed by GitHub
parent 7944aacafa
commit be5f830878
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -217,6 +217,14 @@ class ZhipuEmbed(Base):
def encode(self, texts: list):
arr = []
tks_num = 0
MAX_LEN = -1
if self.model_name.lower() == "embedding-2":
MAX_LEN = 512
if self.model_name.lower() == "embedding-3":
MAX_LEN = 3072
if MAX_LEN > 0:
texts = [truncate(t, MAX_LEN) for t in texts]
for txt in texts:
res = self.client.embeddings.create(input=txt,
model=self.model_name)