Spaces:
Runtime error
Runtime error
File size: 3,231 Bytes
0995744 9ebbf3c 0995744 9ebbf3c 0995744 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | from langchain.chat_models import ChatOpenAI
from langchain.schema import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
from langchain.prompts import ChatPromptTemplate
class Bot():
def __init__(self,template,model='gpt-3.5-turbo-1106'):
self.template = template
self.llm = ChatOpenAI(
model=model,
streaming=True,
temperature=0,
)
self.prompt = ChatPromptTemplate.from_messages(
[
("system", self.template),
("human", "{question}"),
]
)
self.runner = self.prompt | self.llm |StrOutputParser()
def __call__(self,question,context,stream=False):
prompt = self.prompt.format(question=question,context=context)
inputs = {"context":context,"question":question}
if stream:
res = self.runner.stream(inputs)
else:
res = self.runner.invoke(inputs)
return res
def custom_call(self,**kwargs):
return self.runner.invoke(kwargs)
class SearchBot(Bot):
TEMPLATE="""\
You are an expert tasked with summarzing search result and answering questions about FileCoin.
Generate a comprehensive and informative answer of 80 words or less for the \
given question based solely on the provided search results in the context. You must \
only use information from the provided search results.
Anything between the following `context` html blocks is the search result retrieved from a knowledge \
bank.
<context>
{context}
<context/>
"""
def __init__(self):
super().__init__(self.TEMPLATE)
class SummerizeBot(Bot):
TEMPLATE = """
You are an expert tasked with summarzing description for documents.
Generate a comprehensive and informative description of 50 words or less based on the context.
Anything between the following `context` html blocks is the content of the documents.
<context>
{context}
<context/>
Remember to return the description only.
"""
def __init__(self):
super().__init__(template=self.TEMPLATE)
class SearchBotV2(Bot):
TEMPLATE = """
You are an expert tasked with summarizing search results and answering questions about FileCoin.
Generate a comprehensive and informative answer of 80 words or less for the
given question based solely on the provided search results in the context. You must
only use information from the provided search results. Include the most relevant link from the search results
to support your answer.
Below are the structured search results retrieved from a knowledge bank. Each entry consists of a description and associated links.
{context}
Your task is to provide a precise and concise response to the question, and also include the most relevant link from the search results that substantiates your answer.
"""
def __init__(self):
super().__init__(self.TEMPLATE)
search_bot = SearchBot()
search_bot_v2 = SearchBotV2()
summerize_bot = SummerizeBot()
|