Spaces:
Running
Running
File size: 4,944 Bytes
1ab26fb |
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
import gradio as gr
import llama_index
import openai
import phoenix as px
from llama_index import ServiceContext, VectorStoreIndex
from llama_index import set_global_service_context
from llama_index.agent import OpenAIAgent
from llama_index.chat_engine.types import ChatMode
from llama_index.ingestion import IngestionPipeline
from llama_index.tools import QueryEngineTool
from llama_index.vector_stores.qdrant import QdrantVectorStore
from chatbot import Chatbot, IndexBuilder
from constants import CHAT_TEXT_QA_PROMPT, TEXT_QA_SYSTEM_PROMPT, CHUNK_SIZE, DEFAULT_MODEL, \
IS_LOAD_FROM_VECTOR_STORE
from environments import OPENAI_API_KEY, QDRANT_COLLECTION_NAME
from qdrant import client as qdrant_client
from service_provider_config import get_service_provider_config
px.launch_app()
llama_index.set_global_handler("arize_phoenix")
openai.api_key = OPENAI_API_KEY
llm, embedding_model = get_service_provider_config(model_name=DEFAULT_MODEL)
service_context = ServiceContext.from_defaults(
chunk_size=CHUNK_SIZE,
llm=llm,
embed_model=embedding_model,
)
set_global_service_context(service_context)
class KipIndexBuilder(IndexBuilder):
def _load_documents(self):
# TODO: implement logic to import documents into qdrant - API feeding logic to consider
pass
def _setup_service_context(self):
super()._setup_service_context()
def _setup_vector_store(self):
self.vector_store = QdrantVectorStore(
client=qdrant_client, collection_name=self.vdb_collection_name)
super()._setup_vector_store()
def _setup_index(self):
super()._setup_index()
if self.is_load_from_vector_store:
self.index = VectorStoreIndex.from_vector_store(self.vector_store)
print("set up index from vector store")
return
pipeline = IngestionPipeline(
transformations=[
self.embed_model,
],
vector_store=self.vector_store,
)
pipeline.run(documents=self.documents, show_progress=True)
self.index = VectorStoreIndex.from_vector_store(self.vector_store)
class KipToolChatbot(Chatbot):
DENIED_ANSWER_PROMPT = ""
SYSTEM_PROMPT = ""
CHAT_EXAMPLES = []
def _setup_observer(self):
pass
def _setup_index(self):
super()._setup_index()
def _setup_query_engine(self):
super()._setup_query_engine()
self.query_engine = self.index.as_query_engine(
text_qa_template=CHAT_TEXT_QA_PROMPT)
def _setup_tools(self):
super()._setup_tools()
self.tools = QueryEngineTool.from_defaults(
query_engine=self.query_engine)
def _setup_chat_engine(self):
super()._setup_chat_engine()
self.chat_engine = OpenAIAgent.from_tools(
tools=[self.tools],
llm=llm,
similarity_top_k=1,
verbose=True
)
class KipContextChatbot(KipToolChatbot):
def _setup_chat_engine(self):
self.chat_engine = self.index.as_chat_engine(
chat_mode=ChatMode.CONTEXT,
similarity_top_k=5,
system_prompt=TEXT_QA_SYSTEM_PROMPT.content,
text_qa_template=CHAT_TEXT_QA_PROMPT)
class KipSimpleChatbot(KipToolChatbot):
def _setup_chat_engine(self):
self.chat_engine = self.index.as_chat_engine(
chat_mode=ChatMode.SIMPLE)
index_builder = KipIndexBuilder(vdb_collection_name=QDRANT_COLLECTION_NAME,
embed_model=embedding_model,
is_load_from_vector_store=IS_LOAD_FROM_VECTOR_STORE)
kip_chatbot = KipToolChatbot(model_name=DEFAULT_MODEL, index_builder=index_builder)
kip_chatbot_context = KipContextChatbot(model_name=DEFAULT_MODEL, index_builder=index_builder)
kip_chatbot_simple = KipSimpleChatbot(model_name=DEFAULT_MODEL, index_builder=index_builder)
def vote(data: gr.LikeData):
if data.liked:
gr.Info("You up-voted this response: " + data.value)
else:
gr.Info("You down-voted this response: " + data.value)
chatbot = gr.Chatbot()
with gr.Blocks() as demo:
gr.Markdown("# Kipwise LLM demo")
with gr.Tab("Using relevant context sent to system prompt"):
context_interface = gr.ChatInterface(
kip_chatbot_context.stream_chat,
examples=kip_chatbot.CHAT_EXAMPLES,
)
chatbot.like(vote, None, None)
with gr.Tab("Using function calling as tool to retrieve"):
function_call_interface = gr.ChatInterface(
kip_chatbot.stream_chat,
examples=kip_chatbot.CHAT_EXAMPLES,
)
chatbot.like(vote, None, None)
with gr.Tab("Vanilla ChatGPT without modification"):
vanilla_interface = gr.ChatInterface(
kip_chatbot_simple.stream_chat,
examples=kip_chatbot.CHAT_EXAMPLES)
demo.queue(False).launch(server_name='0.0.0.0', share=False)
|