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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import chromadb from chromadb.config import Settings import ollama
chroma_client = chromadb.PersistentClient(path="./chromadb")
documents = [ { "page_content": "合同是两方或多方之间的法律协议,通常包括各方的权利和义务。合同必须具备合法性和可执行性。", "metadata": {"id": "doc1"} }, { "page_content": "在合同中,主要义务包括:1) 付款义务,2) 商品交付义务,3) 相关服务的提供。合同中的这些义务必须在约定的时间内履行。", "metadata": {"id": "doc2"} }, { "page_content": "合同的解除通常需要双方的同意,或者由于法律规定的特殊情况,如违约或不可抗力事件。", "metadata": {"id": "doc3"} }, { "page_content": "违约责任是指一方未能履行合同义务时,应承担的法律后果,通常包括赔偿损失和继续履行合同的责任。", "metadata": {"id": "doc4"} }, { "page_content": "在合同生效之前,所有相关方必须理解合同条款,并同意其内容。签字是合同生效的重要标志。", "metadata": {"id": "doc5"} }, { "page_content": "合约的履行必须符合诚信原则,即各方应诚实守信地履行自己的义务,并尊重对方的合法权益。", "metadata": {"id": "doc6"} }, { "page_content": "在合同争议中,双方可通过调解、仲裁或诉讼的方式解决争端。选择合适的方式取决于争议的性质及金额。", "metadata": {"id": "doc7"} }, { "page_content": "关于合同的法律法规各国有所不同,了解适用的法律条款是签订合同前的重要步骤。", "metadata": {"id": "doc8"} } ]
ollama_client = ollama.Client(host='http://192.168.100.135:11434')
documentation_collection = chroma_client.get_or_create_collection(name="legal_docs")
for doc in documents: response = ollama_client.embeddings( model='bge-m3:567m', prompt=doc['page_content'] ) embedding = response['embedding'] documentation_collection.add( ids=[doc['metadata']['id']], embeddings=[embedding], documents=[doc['page_content']] )
query = "合同中的主要义务是什么?"
query_response = ollama_client.embeddings( model='bge-m3:567m', prompt=query ) query_embedding = query_response['embedding']
results = documentation_collection.query( query_embeddings=[query_embedding], n_results=1 )
data = results['documents'][0] document_content = data
prompt = f"根据以下信息,请回答:{query}"
output = ollama_client.chat(model='llama3.1:8b', messages=[ { 'role': 'user', 'content': f"使用以下数据:{document_content}. 响应这个提示:{prompt}" }, ])
print("生成的结果:", output['message']['content'])
|