Views
No views yet
1from huggingface_hub import hf_hub_download
2from llama_cpp import Llama
3
4model_path = hf_hub_download(
5 repo_id="seasparks/Llama-3.1-8B-Instruct-LegalCite-gguf",
6 filename="Llama-3.1-8B-Instruct-LegalCite-q4_k.gguf"
7)
8
9llm = Llama(model_path=model_path)
10
11sys_message = {"role": "system", "content": "You are an expert assistant answering questions about texts only by accurately citing and providing direct quotes from the text."}
12
13example_text = """(3)
14AI systems can be easily deployed in a large variety of sectors of the economy and many parts of society, including
15across borders, and can easily circulate throughout the Union. Certain Member States have already explored the
16adoption of national rules to ensure that AI is trustworthy and safe and is developed and used in accordance with
17fundamental rights obligations. Diverging national rules may lead to the fragmentation of the internal market and
18may decrease legal certainty for operators that develop, import or use AI systems. A consistent and high level of
19protection throughout the Union should therefore be ensured in order to achieve trustworthy AI, while divergences
20hampering the free circulation, innovation, deployment and the uptake of AI systems and related products and
21services within the internal market should be prevented by laying down uniform obligations for operators and
22guaranteeing the uniform protection of overriding reasons of public interest and of rights of persons throughout the
23internal market on the basis of Article 114 of the Treaty on the Functioning of the European Union (TFEU). To the
24extent that this Regulation contains specific rules on the protection of individuals with regard to the processing of
25personal data concerning restrictions of the use of AI systems for remote biometric identification for the purpose of
26law enforcement, of the use of AI systems for risk assessments of natural persons for the purpose of law
27enforcement and of the use of AI systems of biometric categorisation for the purpose of law enforcement, it is
28appropriate to base this Regulation, in so far as those specific rules are concerned, on Article 16 TFEU. In light of
29those specific rules and the recourse to Article 16 TFEU, it is appropriate to consult the European Data Protection
30Board."""
31
32question = "How will trustworthy AI be achieved?"
33
34messages = [sys_message,
35 {"role": "user", "content": example_text + "\nQuestion: " + question}]
36
37response = llm.create_chat_completion(
38 messages=messages,
39 max_tokens=100,
40)
41
42# Print the assistant's reply
43print(response['choices'][0]['message']['content'])
44
45# Example output (actual model behavior may vary):
46#
47# Q: "How will trustworthy AI be achieved?"
48# A: "...A consistent and high level of protection throughout the Union should therefore be ensured in order to achieve trustworthy AI, while divergences hampering the free circulation, innovation, deployment and the uptake of AI systems and related products and services within the internal market should be prevented..."1# Create Ollama local model with the Modelfile from the repository
2ollama create Llama-3.1-8B-Instruct-LegalCite-q4_k -f Modelfile1from langchain_ollama import ChatOllama
2
3llm = ChatOllama(
4 model = "Llama-3.1-8B-Instruct-LegalCite-q4_k",
5 num_predict = 100,
6)
7
8sys_message = {"role": "system", "content": "You are an expert assistant answering questions about texts only by accurately citing and providing direct quotes from the text."}
9
10example_text = """(3)
11AI systems can be easily deployed in a large variety of sectors of the economy and many parts of society, including
12across borders, and can easily circulate throughout the Union. Certain Member States have already explored the
13adoption of national rules to ensure that AI is trustworthy and safe and is developed and used in accordance with
14fundamental rights obligations. Diverging national rules may lead to the fragmentation of the internal market and
15may decrease legal certainty for operators that develop, import or use AI systems. A consistent and high level of
16protection throughout the Union should therefore be ensured in order to achieve trustworthy AI, while divergences
17hampering the free circulation, innovation, deployment and the uptake of AI systems and related products and
18services within the internal market should be prevented by laying down uniform obligations for operators and
19guaranteeing the uniform protection of overriding reasons of public interest and of rights of persons throughout the
20internal market on the basis of Article 114 of the Treaty on the Functioning of the European Union (TFEU). To the
21extent that this Regulation contains specific rules on the protection of individuals with regard to the processing of
22personal data concerning restrictions of the use of AI systems for remote biometric identification for the purpose of
23law enforcement, of the use of AI systems for risk assessments of natural persons for the purpose of law
24enforcement and of the use of AI systems of biometric categorisation for the purpose of law enforcement, it is
25appropriate to base this Regulation, in so far as those specific rules are concerned, on Article 16 TFEU. In light of
26those specific rules and the recourse to Article 16 TFEU, it is appropriate to consult the European Data Protection
27Board."""
28
29question = "How will trustworthy AI be achieved?"
30
31messages = [sys_message,
32 {"role": "user", "content": example_text + "\nQuestion: " + question}]
33
34print(llm.invoke(messages).content)
35
36# Example output (actual model behavior may vary):
37#
38# Q: "How will trustworthy AI be achieved?"
39# A: "...A consistent and high level of protection throughout the Union should therefore be ensured in order to achieve trustworthy AI, while divergences hampering the free circulation, innovation, deployment and the uptake of AI systems and related products and services within the internal market should be prevented..."meta-llama/Meta-Llama-3.1-8B-Instruct