-
Notifications
You must be signed in to change notification settings - Fork 3
/
langsmith_testing.py
63 lines (49 loc) · 1.84 KB
/
langsmith_testing.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
from dotenv import load_dotenv
from langchain_openai import OpenAIEmbeddings,ChatOpenAI
from langchain_community.vectorstores import Chroma
from langchain.prompts import PromptTemplate
from langchain.chains.question_answering import load_qa_chain
from langsmith import traceable, Client
load_dotenv()
@traceable
def init_vectorstore():
'''Initializes vectorstore'''
return Chroma(
embedding_function=OpenAIEmbeddings(),
persist_directory="./.chromadb",
)
@traceable
def init_llm():
'''Initializes llm'''
return ChatOpenAI(model='gpt-3.5-turbo',temperature=0)
@traceable
def search_vectorstore(vectorstore, email: str):
'''Performs search on vectorstore and returns relevant documents'''
return vectorstore.similarity_search(email)
@traceable
def init_prompt():
'''Initializes system prompt'''
rag_prompt = '''
Task: Write an email response to the following email from a student with answers to their questions given the following context.
Email: {email}
Context: {context}
'''
return PromptTemplate(template=rag_prompt, input_variables=["context", "email"])
@traceable
def generate_response(llm, prompt, email, docs):
'''Generates a response for email, given prompt and relevant documents'''
chain = load_qa_chain(llm, chain_type="stuff", prompt=prompt)
response = chain.invoke(
{"input_documents": docs, "email": email}, return_only_outputs=True
)
return response
if __name__ == "__main__":
# modify the email variable to test different responses
email = "What classes do I have to take to complete the Master's program?"
client = Client()
vectorstore = init_vectorstore()
llm = init_llm()
docs = search_vectorstore(vectorstore, email)
prompt = init_prompt()
response = generate_response(llm, prompt, email, docs)
print(response)