-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.py
More file actions
32 lines (26 loc) · 983 Bytes
/
response.py
File metadata and controls
32 lines (26 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from llama_cpp import Llama
import os
def ask_ai(prompt, tokens, n_threads=4, conversation_history=None):
model_path = "./models/mistral-7b-instruct-v0.1/mistral-7b-instruct-v0.1.Q4_K_M.gguf" # . for source and .. for build
llm = Llama(
model_path=model_path,
n_ctx=4096,
n_threads=n_threads,
n_gpu_layers=0,
verbose=False
)
conversation_text = ""
if conversation_history:
for message in conversation_history:
if message['role'] == 'user':
conversation_text += f"<|user|>\n{message['content']}<|end|>\n"
elif message['role'] == 'assistant':
conversation_text += f"<|assistant|>\n{message['content']}<|end|>\n"
conversation_text += f"<|user|>\n{prompt}<|end|>\n<|assistant|>"
output = llm(
conversation_text,
max_tokens=tokens,
stop=["<|end|>"],
echo=False
)
return output['choices'][0]['text']