Upload llm_classifier_service.py with huggingface_hub
Browse files- llm_classifier_service.py +35 -0
llm_classifier_service.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.llms.ollama import Ollama
|
| 2 |
+
# from langchain_community.llms import Ollama
|
| 3 |
+
from langchain_core.prompts import PromptTemplate
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
PROMPT_TEMPLATE = """Examples
|
| 7 |
+
|
| 8 |
+
Document: USC is one of the top universities in America
|
| 9 |
+
Topic: Universities
|
| 10 |
+
Answer: Yes
|
| 11 |
+
|
| 12 |
+
Document: USC is one of the top universities in America
|
| 13 |
+
Topic: Politics
|
| 14 |
+
Answer: No
|
| 15 |
+
|
| 16 |
+
Document: Governments worldwide are grappling with the challenges of regulating artificial intelligence to ensure it benefits society while protecting individual privacy.
|
| 17 |
+
Topic: Politics
|
| 18 |
+
Answer: Yes
|
| 19 |
+
|
| 20 |
+
Answer if the Topic Classification of the following Document is correct.
|
| 21 |
+
Answer only in "Yes" or "No".
|
| 22 |
+
|
| 23 |
+
Document: {document}
|
| 24 |
+
Topic: {topic}
|
| 25 |
+
Answer:"""
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def topic_in_text_service(document: str, topic: str, model_id: str, base_url: str) -> bool:
|
| 30 |
+
llm = Ollama(model="llama3", base_url="http://ollama:11434")
|
| 31 |
+
prompt_template = PromptTemplate.from_template(PROMPT_TEMPLATE)
|
| 32 |
+
|
| 33 |
+
prompt = prompt_template.format(document=document, topic=topic)
|
| 34 |
+
|
| 35 |
+
return 'yes' in llm.invoke(prompt, num_predict=1).lower()
|