Когда я транслирую ответ mistral7b LLM с помощью Ollama, слева от самого первого фрагмента потоковой передачи появляется дополнительное пространство. Ниже мой код:
import ollama
stream = ollama.chat(
model='mistral',
messages=[{'role': 'user', 'content': 'Name an engineer that passes the vibe check'}],
stream=True
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
Вывод выглядит следующим образом:
$ python3 test.py
Elon Musk, the CEO of SpaceX and Tesla, is an engineer who seems to pass the "vibe check." He is known for his innovative ideas in renewable energy, space travel, and transportation. However, it's important to remember that personality and vibes can be subjective, so not everyone may agree with this assessment. Additionally, Musk's public image should not overshadow the contributions of countless other engineers who are equally impressive but less well-known.
Обратите внимание на самое первое пустое место перед буквой «Е». Как мне удалить его?
Используйте lstrip()
, чтобы удалить пустое пространство.
import ollama
stream = ollama.chat(
model='mistral',
messages=[{'role': 'user', 'content': 'Name an engineer that passes the vibe check'}],
stream=True
)
first = True
for chunk in stream:
if first:
chunk = chunk['message']['content'].lstrip()
print(chunk)
first = False
else:
chunk = chunk['message']['content']
print(chunk)