Spaces:
Runtime error
Runtime error
Create llama_cpp_chains.py
Browse files- src/llama_cpp_chains.py +38 -0
src/llama_cpp_chains.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.llms import LlamaCpp
|
| 2 |
+
from langchain.prompts import PromptTemplate
|
| 3 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 4 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
+
from langchain_core.runnables import RunnableSequence
|
| 6 |
+
|
| 7 |
+
from src.utils import load_config
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class LlamaChain:
|
| 11 |
+
def __init__(self, chat_memory) -> None:
|
| 12 |
+
prompt = PromptTemplate(
|
| 13 |
+
template="""<|begin_of_text|>
|
| 14 |
+
<|start_header_id|>system<|end_header_id|>
|
| 15 |
+
You are a helpful and knowledgeable AI assistant.
|
| 16 |
+
<|eot_id|>
|
| 17 |
+
<|start_header_id|>user<|end_header_id|>
|
| 18 |
+
Previous conversation={chat_history}
|
| 19 |
+
Question: {input}
|
| 20 |
+
Answer: <|eot_id|><|start_header_id|>assistant<|end_header_id|>""",
|
| 21 |
+
input_variables=['chat_history', 'input']
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
self.memory = ConversationBufferWindowMemory(
|
| 25 |
+
memory_key='chat_history',
|
| 26 |
+
chat_memory=chat_memory,
|
| 27 |
+
k=3,
|
| 28 |
+
return_messages=True
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
config = load_config()
|
| 32 |
+
llm = LlamaCpp(**config['chat_model'])
|
| 33 |
+
|
| 34 |
+
self.llm_chain = RunnableSequence(prompt | llm | self.memory | StrOutputParser())
|
| 35 |
+
|
| 36 |
+
def run(self, user_input):
|
| 37 |
+
response = self.llm_chain.invoke(user_input)
|
| 38 |
+
return response['text']
|