Sanchovegapunk commited on
Commit
1cc6bcd
1 Parent(s): 9d9e69a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import time
4
+ from threading import Lock, Thread
5
+ from typing import Optional, Tuple
6
+
7
+ import gradio as gr
8
+ from langchain.agents import AgentType, Tool, initialize_agent
9
+ from langchain.agents.agent_toolkits import (
10
+ create_conversational_retrieval_agent, create_retriever_tool)
11
+ from langchain.callbacks.manager import CallbackManager
12
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
13
+ from langchain.chains import ConversationChain, RetrievalQA
14
+ from langchain.chat_models import ChatOpenAI
15
+ from langchain.embeddings import HuggingFaceBgeEmbeddings
16
+ from langchain.llms import HuggingFaceTextGenInference, OpenAI
17
+ from langchain.prompts import PromptTemplate
18
+ from langchain.schema.messages import SystemMessage
19
+ from langchain.tools import tool
20
+ from langchain.vectorstores import FAISS
21
+ from pydantic import BaseModel, Field
22
+
23
+
24
+ def reset_textbox():
25
+ return gr.update(value='')
26
+
27
+
28
+ model_name = "BAAI/bge-base-en"
29
+
30
+ encode_kwargs = {'normalize_embeddings': True}
31
+
32
+ model_norm = HuggingFaceBgeEmbeddings(
33
+ model_name=model_name,
34
+ encode_kwargs=encode_kwargs
35
+ )
36
+
37
+
38
+ vectordb = FAISS.load_local('faissdb', embeddings=model_norm)
39
+ retriever = vectordb.as_retriever(
40
+ search_type='similarity', search_kwargs={"k": 2})
41
+
42
+ prompt_template = """You are an expert legal assistant with extensive knowledge about Indian law. Your task is to respond to the given query in a factually correct and consise manner unless asked for a detailed explanation. Assume the query is asked by a common man unless explicitly specified otherwise, therefore no special acts or laws like ones for railway , army , police would apply to them. Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
43
+ {context}
44
+ Question: {question}
45
+ Response:"""
46
+
47
+
48
+ PROMPT = PromptTemplate(
49
+ template=prompt_template, input_variables=["context", "question"]
50
+ )
51
+
52
+
53
+ class SearchInput(BaseModel):
54
+ query: str = Field(description="should be a search query in string format")
55
+
56
+
57
+ @tool('search', args_schema=SearchInput)
58
+ def search(query: str) -> str:
59
+ """Useful for retrieving documents related to Indian law."""
60
+ retriever = vectordb.as_retriever(
61
+ search_type='similarity', search_kwargs={"k": 2})
62
+ res = retriever.get_relevant_documents(query)
63
+ print(res)
64
+ return res
65
+
66
+
67
+ def load_chain():
68
+ # tool = create_retriever_tool(
69
+ # retriever,
70
+ # "search_legal_sections",
71
+ # "Searches and returns documents regarding Indian law. Accepts query as a string. For example: 'Section 298 of Indian Penal Code'."
72
+ # )
73
+ tools = [search]
74
+ llm = ChatOpenAI(openai_api_base='http://20.124.240.6:8080/v1',
75
+ openai_api_key='none',)
76
+
77
+ conv_agent_executor = create_conversational_retrieval_agent(
78
+ llm, tools, verbose=False,
79
+ system_message=SystemMessage(
80
+ content="Your name is Votum, an expert legal assistant with extensive knowledge about Indian law. Your task is to respond to the given query in a factually correct and concise manner unless asked for a detailed explanation. Feel free to use any tools available to look up relevant information, only if necessary")
81
+ )
82
+ return conv_agent_executor
83
+
84
+
85
+ with gr.Blocks() as demo:
86
+ chatbot = gr.Chatbot()
87
+ msg = gr.Textbox()
88
+ clear = gr.ClearButton([msg, chatbot])
89
+ chain = load_chain()
90
+
91
+ # def respond(message, chat_history):
92
+ # print('message is', message)
93
+ # bot_message = chain({'input': message})['output']
94
+ # chat_history.append((message, bot_message))
95
+ # time.sleep(2)
96
+ # return "", chat_history
97
+
98
+ def user(user_message, history):
99
+ return "", history + [[user_message, None]]
100
+
101
+ def respond(history):
102
+ print('message is', history[-1])
103
+ bot_message = chain({'input': history[-1][0]})['output']
104
+
105
+ if 'Final answer:' in bot_message:
106
+ bot_message = bot_message.split('Final answer:')[-1]
107
+
108
+ history[-1][1] = bot_message
109
+ # for character in bot_message:
110
+ # history[-1][1] += character
111
+ # time.sleep(0.0)
112
+ # yield history
113
+ return history
114
+
115
+ clear.click(chain.memory.clear(),)
116
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False
117
+ ).then(respond, chatbot, chatbot)
118
+
119
+ if __name__ == "__main__":
120
+ demo.queue(max_size=32).launch()