Chapter 104
ollama
NotebookPython 3 (ipykernel)9 cells
In [2]python · cell 1
python
!rm -f minsearch.py
!wget https://raw.githubusercontent.com/alexeygrigorev/minsearch/main/minsearch.pyOutput
--2024-06-13 13:53:24-- https://raw.githubusercontent.com/alexeygrigorev/minsearch/main/minsearch.py
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.110.133, 185.199.108.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3832 (3.7K) [text/plain]
Saving to: 'minsearch.py'
0K ... 100% 579K=0.006s
2024-06-13 13:53:24 (579 KB/s) - 'minsearch.py' saved [3832/3832]
In [3]python · cell 2
python
import requests
import minsearch
docs_url = 'https://github.com/DataTalksClub/llm-zoomcamp/blob/main/01-intro/documents.json?raw=1'
docs_response = requests.get(docs_url)
documents_raw = docs_response.json()
documents = []
for course in documents_raw:
course_name = course['course']
for doc in course['documents']:
doc['course'] = course_name
documents.append(doc)
index = minsearch.Index(
text_fields=["question", "text", "section"],
keyword_fields=["course"]
)
index.fit(documents)Output
<minsearch.Index at 0x1d9c9bd8890>
In [4]python · cell 3
python
def search(query):
boost = {'question': 3.0, 'section': 0.5}
results = index.search(
query=query,
filter_dict={'course': 'data-engineering-zoomcamp'},
boost_dict=boost,
num_results=5
)
return resultsIn [8]python · cell 4
python
def build_prompt(query, search_results):
prompt_template = """
You're a course teaching assistant. Answer the QUESTION based on the CONTEXT from the FAQ database.
Use only the facts from the CONTEXT when answering the QUESTION.
QUESTION: {question}
CONTEXT:
{context}
""".strip()
context = ""
for doc in search_results:
context = context + f"section: {doc['section']}\nquestion: {doc['question']}\nanswer: {doc['text']}\n\n"
prompt = prompt_template.format(question=query, context=context).strip()
return prompt
def llm(prompt):
response = client.chat.completions.create(
model='phi3',
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.contentIn [6]python · cell 5
python
def rag(query):
search_results = search(query)
prompt = build_prompt(query, search_results)
answer = llm(prompt)
return answerIn [7]python · cell 6
python
from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1/',
api_key='ollama',
)In [12]python · cell 7
python
llm('write that this is a test')Output
' This statement serves as an example to verify the functionality of various systems, such as text processing software or programming functions. It\'s commonly used by developers during debugging sessions to ensure commands are working correctly without producing any unintended output.\n\nHere\'s how you might include it in different contexts:\n\n**1. Using it as a command line test in a script:**\nIf writing a shell script or using command-line tools, the statement can be inserted directly to demonstrate functionality. For instance, using `echo` on Unix-like systems:\n```bash\n#!/bin/bash\necho "This is a test"\necho "This is also a test for confirmation."\n```\n\n**2. Inserting it into a programming function as a placeholder or comment (in Python):**\nAs a comment in code to remind future developers that the block can be replaced with actual implementation:\n```python\ndef process_text(input_string):\n # Test input: "This is a test"\n print("Testing...")\n # Replace this line with your processing logic\n return input_string.upper() # Example operation\n```\n\n**3. Using in documentation or comments within software development code:**\nDemonstrate how the statement can be used to clarify intentions when developing software, such as in a README file or inline comment:\n```markdown\n# Test Command Functionality\nThis section contains commands that serve to test system functionality.\n`echo "This is a test"` - A simple command to check output behavior.\n```\n\nIn each case, the statement `This is a test` fulfills its role as a straightforward demonstration or placeholder within development and testing workflows.'In [13]python · cell 8
python
print(_)Output
This statement serves as an example to verify the functionality of various systems, such as text processing software or programming functions. It's commonly used by developers during debugging sessions to ensure commands are working correctly without producing any unintended output.
Here's how you might include it in different contexts:
**1. Using it as a command line test in a script:**
If writing a shell script or using command-line tools, the statement can be inserted directly to demonstrate functionality. For instance, using `echo` on Unix-like systems:
```bash
#!/bin/bash
echo "This is a test"
echo "This is also a test for confirmation."
```
**2. Inserting it into a programming function as a placeholder or comment (in Python):**
As a comment in code to remind future developers that the block can be replaced with actual implementation:
```python
def process_text(input_string):
# Test input: "This is a test"
print("Testing...")
# Replace this line with your processing logic
return input_string.upper() # Example operation
```
**3. Using in documentation or comments within software development code:**
Demonstrate how the statement can be used to clarify intentions when developing software, such as in a README file or inline comment:
```markdown
# Test Command Functionality
This section contains commands that serve to test system functionality.
`echo "This is a test"` - A simple command to check output behavior.
```
In each case, the statement `This is a test` fulfills its role as a straightforward demonstration or placeholder within development and testing workflows.
In [ ]python · cell 9
python
