48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
from typing import Dict, Any
|
|
from langchain.chat_models import ChatOpenAI
|
|
from langchain.prompts import ChatPromptTemplate
|
|
from langchain.chains import LLMChain
|
|
import asyncio
|
|
|
|
class AdaptiveCards:
|
|
def __init__(self):
|
|
self.llm = ChatOpenAI(temperature=0)
|
|
self.prompt = ChatPromptTemplate.from_template("""
|
|
You are a Microsoft Adaptive Card generator. Given a data schema and known values,
|
|
generate an Adaptive Card (v1.3) that asks the user only for missing fields.
|
|
|
|
Use this schema: https://adaptivecards.io/schemas/adaptive-card.json
|
|
Respond only with valid Adaptive Card JSON. Do not include explanations.
|
|
Always include isRequired, and errorMessage in the schema.
|
|
Always include a submit button at the bottom of the card as defined in the schema.
|
|
|
|
### Schema:
|
|
{schema}
|
|
|
|
### Known values:
|
|
{known_values}
|
|
|
|
""")
|
|
self.chain = LLMChain(llm=self.llm, prompt=self.prompt)
|
|
|
|
async def generate_card(self, schema: Dict[str, Any], known_values: Dict[str, Any]) -> Dict[str, Any]:
|
|
loop = asyncio.get_running_loop()
|
|
return await loop.run_in_executor(None, self.chain.run, {
|
|
"schema": schema,
|
|
"known_values": known_values
|
|
})
|
|
|
|
def create_welcome_card(self):
|
|
"""Create a welcome card"""
|
|
return {
|
|
"type": "AdaptiveCard",
|
|
"body": [
|
|
{
|
|
"type": "TextBlock",
|
|
"text": "Welcome to the Housing Bot!",
|
|
"size": "large"
|
|
}
|
|
],
|
|
"version": "1.0"
|
|
}
|