├── app.py └── requirements.txt /app.py: -------------------------------------------------------------------------------- 1 | from langchain.vectorstores import Chroma 2 | from langchain.embeddings import HuggingFaceEmbeddings 3 | from langchain.text_splitter import RecursiveCharacterTextSplitter 4 | from langchain.document_loaders import PyPDFLoader 5 | from langchain.memory import ConversationBufferMemory 6 | from langchain.llms import OpenAI 7 | from langchain.chains import ConversationalRetrievalChain 8 | import openai 9 | import autogen 10 | 11 | 12 | 13 | 14 | #set llm for langchain using model from lmstudio 15 | openai.api_type = "open_ai" 16 | openai.api_base = "http://localhost:1234/v1" 17 | openai.api_key = "NULL" 18 | 19 | #load the pdf file from directory 20 | loaders = [PyPDFLoader('./chat_docs.pdf')] 21 | docs = [] 22 | for file in loaders: 23 | docs.extend(file.load()) 24 | #split text to chunks 25 | text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000) 26 | docs = text_splitter.split_documents(docs) 27 | 28 | #create a vectorstore 29 | vectorstore = Chroma( 30 | collection_name="full_documents", 31 | embedding_function=HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", 32 | model_kwargs={'device': 'cpu'}) 33 | ) 34 | vectorstore.add_documents(docs) 35 | 36 | qa = ConversationalRetrievalChain.from_llm( 37 | OpenAI(temperature=0), 38 | vectorstore.as_retriever(), 39 | memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True) 40 | ) 41 | 42 | 43 | #set config for autogen 44 | config_list = [ 45 | { 46 | "api_base": "http://localhost:1234/v1", 47 | "api_key": "NULL" 48 | } 49 | ] 50 | 51 | #set autogen user agent and assistant agent with function calling 52 | llm_config={ 53 | "request_timeout": 600, 54 | "seed": 42, 55 | "config_list": config_list, 56 | "temperature": 0, 57 | "functions": [ 58 | { 59 | "name": "chat_docs", 60 | "description": "Answer any chat_docs related questions", 61 | "parameters": { 62 | "type": "object", 63 | "properties": { 64 | "question": { 65 | "type": "string", 66 | "description": "The question to ask in relation to chat_docs", 67 | } 68 | }, 69 | "required": ["question"], 70 | }, 71 | } 72 | ], 73 | } 74 | 75 | #the function takes a parameter question,calls the qa chain and answer it by returnin the answer 76 | # from the chain 77 | def chat_docs(question): 78 | response = qa({"question": question}) 79 | return response["answer"] 80 | 81 | 82 | # create an AssistantAgent instance "assistant" 83 | assistant = autogen.AssistantAgent( 84 | name="assistant", 85 | llm_config=llm_config, 86 | ) 87 | 88 | 89 | # create a UserProxyAgent instance "user_proxy" 90 | user_proxy = autogen.UserProxyAgent( 91 | name="user_proxy", 92 | human_input_mode="NEVER", 93 | max_consecutive_auto_reply=1, 94 | code_execution_config={"work_dir": "docs"}, 95 | llm_config=llm_config, 96 | system_message="""Reply TERMINATE if the task has been solved at full satisfaction. 97 | Otherwise, reply CONTINUE, or the reason why the task is not solved yet.""", 98 | function_map={"chat_docs":chat_docs} 99 | ) 100 | 101 | # the assistant receives a message from the user, which contains the task description 102 | user_proxy.initiate_chat( 103 | assistant, 104 | message=""" 105 | Find the answers to the 3 questions below from the chat_docs.pdf and do not write any code. 106 | 107 | 1.Who is the CEO of walmart? 108 | 2.What did Doug McMillon write about walmart. 109 | 3.Write about the Executive Shuffle? 110 | 111 | Start the work now. 112 | """ 113 | ) 114 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | openai 3 | tiktoken 4 | chromadb 5 | pypdf 6 | pyautogen 7 | huggingface_hub 8 | sentence-transformers --------------------------------------------------------------------------------