eivind-n/P360-AI-Help
1
1import gradio as gr2import os3import openai4import chromadb5from chromadb.config import Settings6from langchain.embeddings import OpenAIEmbeddings7 8 9css="""10#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}11"""12 13title = """14<div style="text-align: center;max-width: 700px;">15 <h1>Ask P360 Help • OpenAI</h1>16 <p style="text-align: center;">Ask questions about the P360 documentation.17</div>18"""19 20chroma_client = chromadb.Client(21 Settings(22 chroma_db_impl='duckdb+parquet',23 persist_directory='./vectorstore/',24 )25)26 27db = chroma_client.get_collection(28 name='help-360',29)30 31embeddings_generator = OpenAIEmbeddings(32 openai_api_key=os.getenv('AZURE_OPENAI_KEY'),33 openai_api_base=os.getenv('AZURE_OPENAI_ENDPOINT'),34 openai_api_type='azure',35 openai_api_version='2023-05-15',36 deployment='embedding-test'37)38 39 40def respond(query):41 '''42 OpenAI GPT response.43 '''44 query_embedding = embeddings_generator.embed_query(query)45 results = db.query(46 query_embeddings=query_embedding,47 n_results=1,48 )49 relevant_help = results['documents'][0][0]50 51 openai.api_type = 'azure'52 openai.api_base = os.getenv('AZURE_OPENAI_ENDPOINT')53 openai.api_key = os.getenv('AZURE_OPENAI_KEY')54 openai.api_version = '2023-05-15'55 56 response = openai.ChatCompletion.create(57 engine='entest-gpt-35-turbo',58 messages=[59 {"role": "system", "content": "You are a helpful assistant in the P360 document archiving system. Only provide helpful answers if you have the necessary information, otherwise answer with I have no information about that."},60 {"role": "user", "content": "What do you know about P360?"},61 {"role": "assistant", "content": relevant_help},62 {"role": "user", "content": query},63 ]64 )65 return response['choices'][0]['message']['content']66 67 68with gr.Blocks(css=css) as demo:69 with gr.Column(elem_id="col-container"):70 gr.HTML(title) 71 question = gr.Textbox(label='Question', placeholder='Type your question and hit Enter ')72 answer = gr.Textbox(label='Answer').style(height=350)73 question.submit(respond, [question], [answer])74 75 76demo.launch()