|
|
|
|
|
import os |
|
import sys |
|
from operator import itemgetter |
|
|
|
import gradio as gr |
|
from dotenv import load_dotenv |
|
from langchain.prompts import PromptTemplate |
|
from langchain_community.embeddings import OllamaEmbeddings |
|
from langchain_community.llms import Ollama |
|
from qdrant_client import QdrantClient |
|
from shared import getModel, getEmbeddingsModel, getQdrantClient |
|
|
|
def answer(samplePrompt, useSample, Query): |
|
if useSample: |
|
query = samplePrompt |
|
else: |
|
query = Query |
|
|
|
qClient = getQdrantClient() |
|
|
|
|
|
embeddingsModel = getEmbeddingsModel() |
|
|
|
|
|
model = getModel() |
|
|
|
|
|
|
|
def retriever(text, collection): |
|
results = qClient.search( |
|
collection_name=collection, |
|
query_vector = embeddingsModel.embed_query(text), |
|
limit=10 |
|
) |
|
return results |
|
|
|
|
|
template = """ |
|
Rewrite the prompt. The new prompt must offer a different perspective. |
|
Do not change the meaning. Output only the rewritten prompt with no introduction. |
|
Prompt: {prompt} |
|
""" |
|
prompt = PromptTemplate.from_template(template) |
|
chain = {"prompt": itemgetter("prompt")} | prompt | model |
|
queryExpansion = chain.invoke({"prompt": query}) |
|
|
|
|
|
template = """ |
|
You are an AI assistant. You must determine if the prompt requires code as the answer. |
|
Output a 1 if it is or a 0 if it is not and nothing else. |
|
Prompt: {prompt} |
|
""" |
|
prompt = PromptTemplate.from_template(template) |
|
chain = {"prompt": itemgetter("prompt")} | prompt | model |
|
codingQuestion = chain.invoke({"prompt": query}) |
|
|
|
|
|
relatedCollection = 'Document' |
|
if (codingQuestion == '1'): |
|
relatedCollection = 'Github' |
|
results1 = retriever(query, relatedCollection) |
|
results2 = retriever(queryExpansion, relatedCollection) |
|
|
|
|
|
results = results1+results2 |
|
|
|
|
|
ids = [result.id for result in results] |
|
scores = [result.score for result in results] |
|
topIds = [] |
|
topIndexes = [] |
|
for x in range(3): |
|
maxScore = 0 |
|
maxIndex = 0 |
|
for i in range(len(ids)): |
|
if ids[i] not in topIds and scores[i] > maxScore: |
|
maxScore = scores[i] |
|
maxIndex = i |
|
topIds.append(ids[maxIndex]) |
|
topIndexes.append(maxIndex) |
|
texts = [result.payload['text'] for result in results] |
|
links = [result.payload['link'] for result in results] |
|
topTexts = '' |
|
for index in topIndexes: |
|
print("Top texts: ", texts[index]) |
|
print("Link: ", links[index]) |
|
topTexts += texts[index] |
|
|
|
|
|
if(codingQuestion == '1'): |
|
template = """ |
|
Write code for the following question given the related coding document below. |
|
|
|
Document: {document} |
|
Question: {question} |
|
""" |
|
prompt = PromptTemplate.from_template(template) |
|
else: |
|
template = """ |
|
Answer the question based on the document below. If you can't answer the question, reply "I don't know" |
|
|
|
Document: {document} |
|
Question: {question} |
|
""" |
|
prompt = PromptTemplate.from_template(template) |
|
|
|
|
|
chain = {"document": itemgetter("document"), "question": itemgetter("question")} | prompt | model |
|
return chain.invoke({"document": topTexts, "question": query}) |
|
|
|
|
|
demo = gr.Interface( |
|
fn=answer, |
|
inputs=[ |
|
gr.Dropdown( |
|
["What is ROS?", "Write me code to move a robot"], label="Sample Prompt" |
|
), |
|
"checkbox", |
|
"text", |
|
], |
|
outputs=["text"], |
|
) |
|
|
|
demo.launch(share=False) |
|
|