pull/1111/head
Rasmus Storjohann 3 months ago
parent 100fe54e01
commit a178e97fef

@ -1,139 +0,0 @@
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_community import embeddings
from langchain_community.chat_models import ChatOllama
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain.text_splitter import CharacterTextSplitter
from langchain.docstore.document import Document
import csv
noc_data = []
with open('data/noc.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
record = {
'code': row['Code - NOC 2021 V1.0'],
'title': row['Class title'],
'definition': row['Class definition']
}
noc_data.append(record)
# with 822 docs nice -n 19 python3 ollama-rag-example.py 7.96s user 1.58s system 13% cpu 1:12.98 total
# with 105 docs: nice -n 19 python3 ollama-rag-example.py 3.17s user 2.18s system 8% cpu 1:00.42 total
def include_page(page):
return page['code'].startswith('2')
def to_page_content(page):
return 'code="' + page['code'] + '" title="' + page['title'] + '" definition="' + page['definition'] + '"'
docs = [[Document(page_content=to_page_content(page)) for page in noc_data if include_page(page)]]
# Sources
# https://www.youtube.com/watch?v=jENqvjpkwmw
model_local = ChatOllama(model="mistral")
# 1. Split data into chucks
urls = [
"https://ollama.com",
"https://ollama.com/blog/windows-preview",
"https://ollama.com/blog/openai-compatibility",
]
# docs = [WebBaseLoader(url).load() for url in urls];
flattened_docs = [item for sublist in docs for item in sublist]
print(flattened_docs)
print('total documents included = ', len(flattened_docs))
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(chunk_size=7500, chunk_overlap=100)
doc_splits = text_splitter.split_documents(flattened_docs)
# 2. Convert documents to Embeddings and store them
vectorstore = Chroma.from_documents(
documents=doc_splits,
collection_name="rag-chroma",
embedding=embeddings.ollama.OllamaEmbeddings(model='nomic-embed-text')
)
retriever = vectorstore.as_retriever()
# 3. Before RAG
if False:
print("Before RAG\n")
before_rag_template = "What is {topic}"
before_rag_prompt = ChatPromptTemplate.from_template(before_rag_template)
before_rag_chain = before_rag_prompt | model_local | StrOutputParser()
print(before_rag_chain.invoke({"topic" : "trademark agents"}))
# 4. After rAG
print("\n###########\nAfter RAG")
after_rag_template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
after_rag_prompt = ChatPromptTemplate.from_template(after_rag_template)
after_rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| after_rag_prompt
| model_local
| StrOutputParser()
)
admin_assistant_js = ("As an Administrative Assistant in the film industry, you will play a pivotal " +
"role in supporting the administrative and organizational functions of film " +
"production companies, studios, or related entities. You will be responsible for " +
" providing comprehensive administrative support to ensure smooth operations and " +
" facilitate the execution of various projects within the dynamic and fast-paced " +
" environment of the film industry.'");
hopital_chef_jd = ("Title: Hospital Chef\n" +
"Job Summary:\n" +
"As a Hospital Chef, you will play a vital role in ensuring the provision of high-quality, nutritious meals for patients, \n" +
"staff, and visitors in a healthcare setting. Working closely with dietitians, nutritionists, and culinary staff, you\n" +
" will be responsible for planning, preparing, and overseeing the production of meals that meet dietary requirements,\n" +
" taste preferences, and nutritional standards.\n" +
"Responsibilities:\n" +
"1. **Menu Planning:** Collaborate with dietitians and nutritionists to plan menus that meet the dietary needs of patients\n" +
" while adhering to medical guidelines and dietary restrictions.\n" +
"2. **Food Preparation:** Prepare and cook meals according to standardized recipes, ensuring consistency in taste, \n" +
"presentation, and portion sizes. Monitor food quality and taste to maintain high standards.\n" +
"3. **Nutritional Considerations:** Ensure that meals are balanced, nutritious, and appropriate for patients with\n" +
" specific medical conditions or dietary restrictions, such as diabetes, food allergies, or heart disease.\n" +
"4. **Food Safety and Sanitation:** Adhere to strict food safety and sanitation protocols to prevent contamination and \n" +
"ensure compliance with health regulations. Monitor kitchen hygiene, equipment maintenance, and food storage practices.\n" +
"5. **Inventory Management:** Oversee inventory levels of food and kitchen supplies, ordering ingredients and supplies \n" +
"as needed to maintain stock levels and minimize waste. Monitor food costs and budgetary constraints.\n" +
"6. **Team Leadership:** Supervise kitchen staff, including cooks, sous chefs, and kitchen assistants, providing \n" +
"guidance, training, and support to ensure efficient operations and teamwork.\n" +
"7. **Special Dietary Needs:** Accommodate special dietary requests from patients, staff, and visitors, \n" +
"including vegetarian, vegan, gluten-free, and other dietary preferences or restrictions.\n" +
"8. **Menu Development:** Continuously evaluate and update menus to incorporate seasonal ingredients, culinary\n" +
" trends, and feedback from patients and staff. Introduce new recipes and dishes to enhance the dining experience.\n" +
"9. **Patient Satisfaction:** Solicit feedback from patients and staff regarding meal quality, preferences,\n" +
" and satisfaction. Implement improvements and adjustments based on feedback to enhance the overall dining experience.\n" +
"10. **Regulatory Compliance:** Ensure compliance with regulatory agencies, such as the Department of Health, Joint Commission, and local health authorities")
geological_engineer_jd = ("""Title: Geological Engineer
Job Summary:
As a Geological Engineer, you will play a crucial role in assessing the geological conditions of sites and providing expertise in engineering projects related to natural resources exploration, environmental protection, infrastructure development, and hazard mitigation. Your responsibilities will involve analyzing geological data, conducting field surveys, and collaborating with multidisciplinary teams to ensure the safe and efficient execution of engineering projects.
Responsibilities:
1. **Site Investigation:** Conduct geological surveys and site investigations to assess geological features, including rock formations, soil composition, groundwater conditions, and potential hazards such as landslides, earthquakes, or sinkholes.
2. **Geological Mapping:** Create detailed geological maps and models using specialized software and mapping techniques to identify geological structures, mineral deposits, and potential risks for engineering projects.
3. **Geotechnical Analysis:** Perform geotechnical analyses to evaluate soil stability, bearing capacity, and slope stability for the design and construction of infrastructure projects, such as buildings, bridges, roads, and dams.
4. **Risk Assessment:** Assess geological risks and hazards associated with engineering projects, including seismic activity, soil erosion, groundwater contamination, and geological instabilities, and develop mitigation strategies to minimize risks.
5. **Environmental Impact Assessment:** Evaluate the environmental impact of engineering activities on natural ecosystems, water resources, and air quality, and recommend measures to mitigate negative impacts and ensure compliance with environmental regulations.
6. **Resource Exploration:** Assist in the exploration and extraction of natural resources, such as minerals, oil, gas, and water, by analyzing geological data, conducting drilling surveys, and identifying potential resource reserves.
7. **Project Planning:** Provide geological input and expertise during the planning and design phases of engineering projects, including site selection, foundation design, and construction techniques, to optimize project outcomes and minimize geological risks.
8. **Data Analysis:** Analyze geological data collected from field surveys, laboratory tests, and remote sensing""")
print(after_rag_chain.invoke("What are the three documents that most closely match this job description: '" + geological_engineer_jd + "'. Answer in JSON format with the top level identifier 'results', and attributes code, title, definition, score and comment for each matching document, where score is a number between 0 and 1 indicating how close the match is to the job description, with 1 meaning really close, and comment explains why this particular documents was selected as a good match."))
Loading…
Cancel
Save