Este repositorio se corresponde con los ejercicios vistos en el Workshop impartido por Gradiant en la HackUDC 2025.
Tras instalar Ollama podemos lanzar el servidor.
ollama serve
La forma más básica de interactuar con los modelos es lanzar uno desde la consola.
ollama run llama3.2:1b
El servidor expone una API REST con la que también podremos interactuar. La API ya nos empieza a ofrecer más flexibilidad para realizar tareas de forma programática.
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2:1b",
"messages": [
{
"role": "user",
"content": "why is the sky blue?"
}
],
"stream": false
}'
pip install requests
import requests
url = 'http://localhost:11434/api/chat'
data = {
"model": "llama3.2:1b",
"messages": [
{
"role": "user",
"content": "why is the sky blue?"
}
],
"stream": False
}
response = requests.post(url, json=data)
print(response.text)
Utilizaremos el SDK de Python: ollama-python
pip install ollama
from ollama import chat
from ollama import ChatResponse
response: ChatResponse = chat(model='llama3.2:1b', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
])
print(response.message.content)
LangChain nos ofrece la posibilidad de hacer pipelines de manera sencilla y tiene integración con Ollama.
pip install langchain
pip install langchain-ollama
from langchain_ollama.chat_models import ChatOllama
model = ChatOllama(model = "llama3.2:1b")
messages = [
{
"role": "user",
"content": "Why is the sky blue?"
}
]
response = model.invoke(messages)
print(response.content)
from langchain_ollama.chat_models import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
model = ChatOllama(model = "llama3.2:1b", temperature = 0)
system_template = "Translate the following from English into {language}"
prompt_template = ChatPromptTemplate.from_messages(
[("system", system_template), ("user", "{text}")]
)
message = {"language": "Spanish", "text": "Car is blue"}
prompt = prompt_template.invoke(message)
response = model.invoke(prompt)
print(response.content)
from langchain_ollama.chat_models import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
model = ChatOllama(model = "llama3.2:1b", temperature = 0)
from pydantic import BaseModel, Field
class ResponseFormatter(BaseModel):
item: str = Field(description="The object refered in the sentence")
color: str = Field(description="The color of the object")
model_with_structure = model.with_structured_output(ResponseFormatter)
message = "The car is blue."
structured_output = model_with_structure.invoke(message)
print(structured_output.model_dump_json())
from langchain_ollama.chat_models import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
model = ChatOllama(model = "llama3.2:1b", temperature = 0)
from pydantic import BaseModel, Field
class ResponseFormatter(BaseModel):
action: str = Field(description="The action that took place")
item: str = Field(description="The element that the action refers to")
date: str = Field(description="Relative days ago when the action took place")
model_with_structure = model.with_structured_output(ResponseFormatter)
message = "I've watched a microservices tutorial two days ago."
structured_output = model_with_structure.invoke(message)
print(structured_output.model_dump_json())
CLOC es una herramienta para contar líneas de código pero nos ofrece la posibilidad de detectar el lenguaje.
cloc README.md
cloc ./ --by-file --csv --quiet --report-file=cloc_report.csv
Git log nos muestra el historial de cambios del repo.
git log
Podemos hacer un listado de personas que han modificado un fichero ...
git log --pretty=format:'%an' -- README.md | sort -u
Git blame nos dice quién ha modificado cada línea de código de un fichero
git blame README.md
Jugando con los parámetros podemos aislar el nombre de quien ha modificado una línea concreta ...
git blame --porcelain -L 5,5 README.md | sed -n 's/^author //p'