forked from mckaywrigley/repo-chat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
82 lines (60 loc) · 2.41 KB
/
main.py
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
import os
from dotenv import load_dotenv
from supabase.client import Client, create_client
from langchain import LLMChain
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
)
from langchain.vectorstores import SupabaseVectorStore
from langchain.schema import (
SystemMessage
)
from langchain.chat_models import ChatOpenAI
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
load_dotenv()
supabase_url = os.environ.get("SUPABASE_URL")
supabase_key = os.environ.get("SUPABASE_SERVICE_KEY")
supabase: Client = create_client(supabase_url, supabase_key)
embeddings = OpenAIEmbeddings()
vector_store = SupabaseVectorStore(
supabase,
embeddings,
table_name=os.environ.get("TABLE_NAME"),
query_name="repo_chat_search"
)
while True:
query = input("\033[34mWhat question do you have about your repo?\n\033[0m")
if query.lower().strip() == "exit":
print("\033[31mGoodbye!\n\033[0m")
break
matched_docs = vector_store.similarity_search(query)
code_str = ""
for doc in matched_docs:
code_str += doc.page_content + "\n\n"
print("\n\033[35m" + code_str + "\n\033[32m")
template="""
You are Codebase AI. You are a superintelligent AI that answers questions about codebases.
You are:
- helpful & friendly
- good at answering complex questions in simple language
- an expert in all programming languages
- able to infer the intent of the user's question
The user will ask a question about their codebase, and you will answer it.
When the user asks their question, you will answer it by searching the codebase for the answer.
Here is the user's question and code file(s) you found to answer the question:
Question:
{query}
Code file(s):
{code}
[END OF CODE FILE(S)]w
Now answer the question using the code file(s) above.
"""
chat = ChatOpenAI(streaming=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), verbose=True, temperature = 0.5)
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt])
chain = LLMChain(llm=chat, prompt=chat_prompt)
chain.run(code=code_str, query=query)
print("\n\n")