Spaces:
Runtime error
Runtime error
Commit
·
c626d36
1
Parent(s):
329ef19
Create app.py
Browse filescreated `app.py`
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain import LLMChain
|
2 |
+
from custom_class import CustomLLM
|
3 |
+
import chainlit as cl
|
4 |
+
from langchain.prompts import (
|
5 |
+
ChatPromptTemplate,
|
6 |
+
SystemMessagePromptTemplate,
|
7 |
+
)
|
8 |
+
|
9 |
+
from metaphor_python import Metaphor
|
10 |
+
|
11 |
+
metaphor = Metaphor("56d9ca28-e84b-43ce-8f68-75e1a8bb4dd3") ## This is metaphor API Key
|
12 |
+
|
13 |
+
|
14 |
+
template = """ You are a helpful AI assistant who is tasked to answer user queries.
|
15 |
+
{question}
|
16 |
+
|
17 |
+
Answer:
|
18 |
+
"""
|
19 |
+
|
20 |
+
|
21 |
+
@cl.on_chat_start
|
22 |
+
async def factory():
|
23 |
+
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
|
24 |
+
|
25 |
+
prompt = ChatPromptTemplate.from_messages([system_message_prompt])
|
26 |
+
llm = CustomLLM()
|
27 |
+
|
28 |
+
llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True,)
|
29 |
+
|
30 |
+
cl.user_session.set("llm_chain", llm_chain)
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
@cl.on_message
|
35 |
+
async def main(message):
|
36 |
+
llm_chain = cl.user_session.get("llm_chain")
|
37 |
+
|
38 |
+
res = await llm_chain.acall(message, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
39 |
+
|
40 |
+
await cl.Message(content=res["text"]).send()
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
@cl.author_rename # This will be particularly useful when we want to customize this thing for production.
|
45 |
+
def rename(orig_author):
|
46 |
+
rename_dict = {
|
47 |
+
'LLMChain': 'Blaze'
|
48 |
+
}
|
49 |
+
return rename_dict.get(orig_author, orig_author)
|