utils
Browse files- chatbot_logic.py +36 -0
chatbot_logic.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
|
| 2 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 3 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def create_chatbot_chain():
|
| 8 |
+
"""
|
| 9 |
+
Creates a modular chatbot using LangChain Runnables
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
# Initialize Hugging Face LLM
|
| 13 |
+
llm = HuggingFaceEndpoint(
|
| 14 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 15 |
+
task="text-generation",
|
| 16 |
+
max_new_tokens=512,
|
| 17 |
+
temperature=0.7,
|
| 18 |
+
token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Define prompt template
|
| 22 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 23 |
+
("system", "You are a helpful AI assistant. You can answer questions, tell jokes, and have friendly conversations."),
|
| 24 |
+
("human", "{input}")
|
| 25 |
+
])
|
| 26 |
+
|
| 27 |
+
# Create Runnable chain
|
| 28 |
+
chain = (
|
| 29 |
+
{"input": RunnablePassthrough()}
|
| 30 |
+
| prompt
|
| 31 |
+
| llm
|
| 32 |
+
| StrOutputParser()
|
| 33 |
+
| RunnableLambda(lambda x: {"output": x})
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return chain
|