Untitled
unknown
python
a month ago
1.9 kB
7
Indexable
Never
import argparse # from dataclasses import dataclass from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings from langchain_openai import ChatOpenAI from langchain.prompts import ChatPromptTemplate from dotenv import load_dotenv from pathlib import Path import os import openai import numpy as np np.float_ = np.float64 CHROMA_PATH = "chroma" PROMPT_TEMPLATE = """ Answer the question based only on the following context: {context} --- Answer the question based on the above context: {question} """ load_dotenv(Path(".env")) # Set OpenAI API key openai_api_key = os.getenv("OPENAI_API_KEY") if openai_api_key is None: raise ValueError("OpenAI API key not found. Make sure it's set in APIKEY.env.") openai.api_key = openai_api_key def main(): # Create CLI. parser = argparse.ArgumentParser() parser.add_argument("query_text", type=str, help="The query text.") args = parser.parse_args() query_text = args.query_text # Prepare the DB. embedding_function = OpenAIEmbeddings() db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function) # Search the DB. results = db.similarity_search_with_relevance_scores(query_text, k=3) # if len(results) == 0 or results[0][1] < 0.7: # print(f"Unable to find matching results.") # return context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results]) prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE) prompt = prompt_template.format(context=context_text, question=query_text) print(prompt) model = ChatOpenAI() response_text = model.predict(prompt) sources = [doc.metadata.get("source", None) for doc, _score in results] formatted_response = f"Response: {response_text}\nSources: {sources}" print(formatted_response) if __name__ == "__main__": main()
Leave a Comment