style: llm -> model (#33423)

This commit is contained in:
Mason Daugherty
2025-10-10 13:19:13 -04:00
committed by GitHub
parent dd994b9d7f
commit 291a9fcea1
67 changed files with 586 additions and 572 deletions

View File

@@ -294,7 +294,7 @@ class ChatOllama(BaseChatModel):
```python
from langchain_ollama import ChatOllama
llm = ChatOllama(
model = ChatOllama(
model="gpt-oss:20b",
validate_model_on_init=True,
temperature=0.8,
@@ -309,7 +309,7 @@ class ChatOllama(BaseChatModel):
("system", "You are a helpful translator. Translate the user sentence to French."),
("human", "I love programming."),
]
llm.invoke(messages)
model.invoke(messages)
```
```python
@@ -318,7 +318,7 @@ class ChatOllama(BaseChatModel):
Stream:
```python
for chunk in llm.stream("Return the words Hello World!"):
for chunk in model.stream("Return the words Hello World!"):
print(chunk.text, end="")
```
@@ -331,7 +331,7 @@ class ChatOllama(BaseChatModel):
```
```python
stream = llm.stream(messages)
stream = model.stream(messages)
full = next(stream)
for chunk in stream:
full += chunk
@@ -359,7 +359,7 @@ class ChatOllama(BaseChatModel):
Async:
```python
await llm.ainvoke("Hello how are you!")
await model.ainvoke("Hello how are you!")
```
```python
@@ -383,7 +383,7 @@ class ChatOllama(BaseChatModel):
```
```python
async for chunk in llm.astream("Say hello world!"):
async for chunk in model.astream("Say hello world!"):
print(chunk.content)
```
@@ -396,7 +396,7 @@ class ChatOllama(BaseChatModel):
```python
messages = [("human", "Say hello world!"), ("human", "Say goodbye world!")]
await llm.abatch(messages)
await model.abatch(messages)
```
```python
@@ -440,8 +440,8 @@ class ChatOllama(BaseChatModel):
JSON mode:
```python
json_llm = ChatOllama(format="json")
llm.invoke(
json_model = ChatOllama(format="json")
json_model.invoke(
"Return a query for the weather in a random location and time of day with two keys: location and time_of_day. "
"Respond using JSON only."
).content
@@ -495,18 +495,18 @@ class ChatOllama(BaseChatModel):
```python
from langchain_ollama import ChatOllama
llm = ChatOllama(
model = ChatOllama(
model="deepseek-r1:8b",
validate_model_on_init=True,
reasoning=True,
)
llm.invoke("how many r in the word strawberry?")
model.invoke("how many r in the word strawberry?")
# or, on an invocation basis:
llm.invoke("how many r in the word strawberry?", reasoning=True)
# or llm.stream("how many r in the word strawberry?", reasoning=True)
model.invoke("how many r in the word strawberry?", reasoning=True)
# or model.stream("how many r in the word strawberry?", reasoning=True)
# If not provided, the invocation will default to the ChatOllama reasoning
# param provided (None by default).
@@ -1327,10 +1327,10 @@ class ChatOllama(BaseChatModel):
)
llm = ChatOllama(model="llama3.1", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)
model = ChatOllama(model="llama3.1", temperature=0)
structured_model = model.with_structured_output(AnswerWithJustification)
structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
# -> AnswerWithJustification(
# answer='They weigh the same',
@@ -1352,13 +1352,13 @@ class ChatOllama(BaseChatModel):
justification: str
llm = ChatOllama(model="llama3.1", temperature=0)
structured_llm = llm.with_structured_output(
model = ChatOllama(model="llama3.1", temperature=0)
structured_model = model.with_structured_output(
AnswerWithJustification,
include_raw=True,
)
structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
# -> {
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
@@ -1385,13 +1385,13 @@ class ChatOllama(BaseChatModel):
)
llm = ChatOllama(model="llama3.1", temperature=0)
structured_llm = llm.with_structured_output(
model = ChatOllama(model="llama3.1", temperature=0)
structured_model = model.with_structured_output(
AnswerWithJustification,
method="function_calling",
)
structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
# -> AnswerWithJustification(
# answer='They weigh the same',
@@ -1414,10 +1414,10 @@ class ChatOllama(BaseChatModel):
justification: Annotated[str | None, None, "A justification for the answer."]
llm = ChatOllama(model="llama3.1", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)
model = ChatOllama(model="llama3.1", temperature=0)
structured_model = model.with_structured_output(AnswerWithJustification)
structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
structured_model.invoke("What weighs more a pound of bricks or a pound of feathers")
# -> {
# 'answer': 'They weigh the same',
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
@@ -1441,10 +1441,10 @@ class ChatOllama(BaseChatModel):
'required': ['answer']
}
llm = ChatOllama(model="llama3.1", temperature=0)
structured_llm = llm.with_structured_output(oai_schema)
model = ChatOllama(model="llama3.1", temperature=0)
structured_model = model.with_structured_output(oai_schema)
structured_llm.invoke(
structured_model.invoke(
"What weighs more a pound of bricks or a pound of feathers"
)
# -> {
@@ -1465,12 +1465,12 @@ class ChatOllama(BaseChatModel):
justification: str
llm = ChatOllama(model="llama3.1", temperature=0)
structured_llm = llm.with_structured_output(
model = ChatOllama(model="llama3.1", temperature=0)
structured_model = model.with_structured_output(
AnswerWithJustification, method="json_mode", include_raw=True
)
structured_llm.invoke(
structured_model.invoke(
"Answer the following question. "
"Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n"
"What's heavier a pound of bricks or a pound of feathers?"

View File

@@ -66,7 +66,7 @@ class OllamaLLM(BaseLLM):
```python
from langchain_ollama import OllamaLLM
llm = OllamaLLM(
model = OllamaLLM(
model="llama3.1",
temperature=0.7,
num_predict=256,
@@ -78,7 +78,7 @@ class OllamaLLM(BaseLLM):
Invoke:
```python
input_text = "The meaning of life is "
response = llm.invoke(input_text)
response = model.invoke(input_text)
print(response)
```
```txt
@@ -88,7 +88,7 @@ class OllamaLLM(BaseLLM):
Stream:
```python
for chunk in llm.stream(input_text):
for chunk in model.stream(input_text):
print(chunk, end="")
```
```txt
@@ -98,10 +98,10 @@ class OllamaLLM(BaseLLM):
Async:
```python
response = await llm.ainvoke(input_text)
response = await model.ainvoke(input_text)
# stream:
# async for chunk in llm.astream(input_text):
# async for chunk in model.astream(input_text):
# print(chunk, end="")
```
"""