Вот фрагмент кода, который я использую для взаимодействия с моделью:
Код: Выделить всё
import psutil
import os
import warnings
from llama_cpp import Llama
# Suppress warnings
warnings.filterwarnings("ignore")
# Path to the model
model_path = "C:/Llama_project/models/llama-2-7b-chat.Q2_K.gguf"
# Load the model
llm = Llama(model_path=model_path)
# System message to set the behavior of the assistant
system_message = "You are a helpful assistant."
# Function to ask questions
def ask_question(question):
# Use user input for the question prompt
prompt = f"Answer the following question: {question}"
# Calculate the remaining tokens for output based on the model's 4096 token limit
prompt_tokens = len(prompt.split()) # Rough token count estimate
max_output_tokens = 4096 - prompt_tokens # Tokens left for output
# Monitor memory usage before calling the model
process = psutil.Process(os.getpid())
mem_before = process.memory_info().rss / 1024 ** 2 # Memory in MB
# Get the output from the model with the calculated max tokens for output
output = llm(prompt=prompt, max_tokens=max_output_tokens, temperature=0.7, top_p=1.0)
# Monitor memory usage after calling the model
mem_after = process.memory_info().rss / 1024 ** 2 # Memory in MB
# Clean the output and return only the answer text
return output["choices"][0]["text"].strip()
# Main loop for user interaction
while True:
user_input = input("Ask a question (or type 'exit' to quit): ")
if user_input.lower() == 'exit':
print("Exiting the program.")
break
# Get the model's response
answer = ask_question(user_input)
# Print only the answer
print(f"Answer: {answer}")
- Модель: Llama 2-7B (версия Q2_K)
- Ожидаемый результат: я ожидал ответа, близкого к максимальному лимиту токенов (3000 или более токенов).
- Фактический результат: вывод ограничен 511 токенами, независимо от запроса длина.
- Установить max_tokens на 3000 или выше.
Расчет доступных токенов путем вычитания длины запроса из общего лимита токенов модели.
Подробнее здесь: https://stackoverflow.com/questions/792 ... 511-tokens