wei12138 commited on
Commit
919a34c
·
1 Parent(s): a7c10b3

Create interact_with_llm.py

Browse files
Files changed (1) hide show
  1. interact_with_llm.py +98 -0
interact_with_llm.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.embeddings import OpenAIEmbeddings
2
+ from langchain.vectorstores import DeepLake
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.schema.runnable import RunnablePassthrough
5
+ from langchain import hub
6
+ import os
7
+ from langchain.document_loaders import TextLoader
8
+ from langchain.text_splitter import CharacterTextSplitter
9
+ from langchain.embeddings import OpenAIEmbeddings
10
+ from langchain.vectorstores import Chroma
11
+
12
+ QUESTION_ANSWER_MAP = {
13
+ "Question1: Are you usually?": {
14
+ "A 'Good Mixer with groups of people": "Extrovert",
15
+ "Rather quiet and reserved": "Introvert"
16
+ },
17
+ "Question2: Among your friends, you are?": {
18
+ "Full of news about everybody": "Extrovert",
19
+ "One of the last to hear what is going on": "Introvert"
20
+ },
21
+ "Question3: In doing something that many other people do, you would rather?": {
22
+ "Invent a way of your own": "Intuition",
23
+ "Do it in the accepted way": "Sensing"
24
+ },
25
+ "Question4: Do you admire the people who are?": {
26
+ "Normal-acting to never make themselves the center of attention": "Sensing",
27
+ "Too original and individual to care whether they are the center of attention or not": "Intuition"
28
+ },
29
+ "Question5: Do you more often let?": {
30
+ "Your heart rule your head": "Feeling",
31
+ "Your head rule your heart": "Thinking"
32
+ },
33
+ "Question6: Do you usually?": {
34
+ "Value emotion more than logic": "Feeling",
35
+ "Value logic more than feelings": "Thinking"
36
+ },
37
+ "Question7: When you go somewhere for the day, you would rather": {
38
+ "Plan what you will do and when": "Judging",
39
+ "Just go": "Perceiving"
40
+ },
41
+ "Question8: When you have a special job to do, you like to": {
42
+ "Organize it carefully before you start": "Judging",
43
+ "Find out what is necessary as you go along": "Perceiving"
44
+ }
45
+ }
46
+
47
+ os.environ['OPENAI_API_KEY']= os.environ.get('OPENAI_API_KEY')
48
+ class Agent():
49
+ def __init__(self) -> None:
50
+
51
+ embeddings = OpenAIEmbeddings()
52
+ documents = TextLoader("document_0.txt").load()
53
+ text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
54
+ docs = text_splitter.split_documents(documents)
55
+ vectorstore = Chroma(embedding_function=OpenAIEmbeddings(), persist_directory="./chroma_db")
56
+ retriever = vectorstore.as_retriever()
57
+ llm = ChatOpenAI(model_name="gpt-4", temperature=0)
58
+ rag_prompt = hub.pull("rlm/rag-prompt")
59
+ self.rag_chain = (
60
+ {"context": retriever, "question": RunnablePassthrough()}
61
+ | rag_prompt
62
+ | llm
63
+ )
64
+
65
+ def send_msg(self,dic):
66
+ for k, v in dic.items():
67
+ original = k
68
+ question= k.split(":")[1].strip()
69
+ ans = v
70
+
71
+ options = list(QUESTION_ANSWER_MAP[original].items())
72
+ optionA = options[0][0]
73
+ optionB = options[1][0]
74
+ desA = options[0][1]
75
+ desB = options[1][1]
76
+
77
+ result = self.rag_chain.invoke( "Given the question: '{}', the possible answers are: "
78
+ "A. '{}' and B. '{}'. Choosing A indicates the person is {} "
79
+ "while B indicates the person is {}. Based on the response: "
80
+ "{}, "
81
+ "determine how much this person aligns with being {} on a scale of 0-100. "
82
+ "0-10 means 'Extremely {}', 11-20 means 'Very {}', 21-45 means '{}', "
83
+ "55-80 means 'Moderately {}', 81-90 means 'Very {}', "
84
+ "91-100 means 'Extremely {}'. Give a number as output, don't give any explanation!"
85
+ "If the answer provided by this person is not related, return 50."
86
+ .format(question, optionA, optionB, desA, desB, ans,desA, desA, desA, desA, desB, desB, desB))
87
+
88
+ return (desA, result.content)
89
+ def send_post(self, post):
90
+ result = self.rag_chain.invoke(
91
+ f"Based on the content of the post '{post}',"
92
+ "provide a guess using the MBTI dimensions. "
93
+ "For each dimension, assign a score from 0 to 100 to indicate the likelihood of the author displaying the first trait over the second."
94
+ "For example, a score of 60 for 'Extrovert-Introvert' would indicate 60% Extrovert and 40% Introvert."
95
+ "Along with the score, provide a brief rationale for your judgment. If the information is not enough, just try to guess, don't worry about the correctness."
96
+ "Return the results in a structured format that includes both the scores and rationales, suitable for parsing with Python's json library.")
97
+ return result.content
98
+