24 lines
839 B
Python
24 lines
839 B
Python
from langchain.chat_models import ChatOpenAI
|
|
from langchain.prompts import ChatPromptTemplate
|
|
from langchain.chains import LLMChain
|
|
import asyncio
|
|
|
|
class IntentDetector:
|
|
def __init__(self, temperature: float = 0.0):
|
|
self.llm = ChatOpenAI(temperature=temperature)
|
|
self.prompt = ChatPromptTemplate.from_template("""
|
|
You are an intent detection bot. Classify the user input into one of the following intents:
|
|
|
|
- Information about house prices
|
|
- unknown
|
|
|
|
If you're unsure, respond with `unknown`.
|
|
|
|
User: {message}
|
|
Intent:""")
|
|
self.chain = LLMChain(llm=self.llm, prompt=self.prompt)
|
|
|
|
async def detect_intent(self, message: str) -> str:
|
|
loop = asyncio.get_running_loop()
|
|
return await loop.run_in_executor(None, self.chain.run, {"message": message})
|