feat: add temperature parameter to Ollama generate_text
Add temperature control for LLM text generation: - temperature=0.0 for deterministic outputs (JSON, rankings) - temperature=0.3-0.5 for controlled creative content - None uses model default (~0.7 for mistral-nemo) Based on llm-findings.md recommendations for improving mistral-nemo output consistency. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -249,7 +249,8 @@ class OllamaClient:
|
|||||||
self,
|
self,
|
||||||
prompt: str,
|
prompt: str,
|
||||||
model: Optional[str] = None,
|
model: Optional[str] = None,
|
||||||
stream: bool = False
|
stream: bool = False,
|
||||||
|
temperature: Optional[float] = None
|
||||||
) -> Optional[str]:
|
) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
Generate text completion (for non-embedding use cases).
|
Generate text completion (for non-embedding use cases).
|
||||||
@@ -258,12 +259,15 @@ class OllamaClient:
|
|||||||
prompt: Input prompt
|
prompt: Input prompt
|
||||||
model: Model name (defaults to self.model)
|
model: Model name (defaults to self.model)
|
||||||
stream: Enable streaming response
|
stream: Enable streaming response
|
||||||
|
temperature: Sampling temperature (0.0 = deterministic, higher = more creative)
|
||||||
|
None uses model default (~0.7 for mistral-nemo)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Generated text or None on failure
|
Generated text or None on failure
|
||||||
|
|
||||||
Note: This is primarily for debugging/testing. Use specialized
|
Note: Use temperature=0.0 for deterministic outputs like JSON parsing,
|
||||||
LLM services for production text generation.
|
ranking, and factual extraction. Use higher values (0.3-0.7) for
|
||||||
|
creative content generation.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
payload = {
|
payload = {
|
||||||
@@ -272,6 +276,10 @@ class OllamaClient:
|
|||||||
"stream": stream
|
"stream": stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Add temperature to options if specified
|
||||||
|
if temperature is not None:
|
||||||
|
payload["options"] = {"temperature": temperature}
|
||||||
|
|
||||||
response = await self.client.post(
|
response = await self.client.post(
|
||||||
self.generate_url,
|
self.generate_url,
|
||||||
json=payload
|
json=payload
|
||||||
|
|||||||
Reference in New Issue
Block a user