added Tokens, openrouter, memory system
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
// nazarick-core/src/llm/mod.rs
|
||||
//
|
||||
// LLM-Modul — Typen und Traits für alle LLM-Provider.
|
||||
// Re-exportiert alles damit Nutzer nur `nazarick_core::llm::X` schreiben müssen.
|
||||
|
||||
mod types;
|
||||
mod traits;
|
||||
|
||||
pub use types::{Message, LlmRequest, LlmResponse};
|
||||
pub use types::{Message, LlmRequest, LlmResponse, ToolCall, ToolCallFunction};
|
||||
pub use traits::{LlmProvider, SkillFormat};
|
||||
@@ -1,34 +1,33 @@
|
||||
// nazarick-core/src/llm/traits.rs
|
||||
//
|
||||
// LlmProvider Trait — gemeinsame Schnittstelle für alle LLM-Backends.
|
||||
// Neue Provider (Ollama, Mistral) implementieren diesen Trait.
|
||||
|
||||
use crate::types::Result;
|
||||
use crate::llm::types::{LlmRequest, LlmResponse};
|
||||
|
||||
/// Format für Skill-Calls das dieser Provider unterstützt.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum SkillFormat {
|
||||
/// XML-Tags — funktioniert mit lokalen Modellen
|
||||
/// <skill name="update_personality">...</skill>
|
||||
/// XML-Tags — für lokale Modelle ohne Function Calling
|
||||
Xml,
|
||||
/// Native Tool Use — Claude, GPT-4, Mistral API
|
||||
/// Strukturierter JSON-basierter Funktionsaufruf
|
||||
/// Native Tool Use — Ollama, Mistral API, OpenRouter
|
||||
ToolUse,
|
||||
/// Skills deaktiviert — Modell folgt keinem Format zuverlässig
|
||||
/// Skills deaktiviert
|
||||
None,
|
||||
}
|
||||
|
||||
impl SkillFormat {
|
||||
/// Parsed aus config.toml String
|
||||
pub fn from_str(s: &str) -> Self {
|
||||
match s {
|
||||
"tool_use" => Self::ToolUse,
|
||||
"none" => Self::None,
|
||||
_ => Self::Xml, // default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait LlmProvider: Send + Sync {
|
||||
/// Sendet eine Anfrage an das LLM und gibt die Antwort zurück.
|
||||
async fn complete(&self, request: LlmRequest) -> Result<LlmResponse>;
|
||||
|
||||
/// Gibt den Namen des Providers zurück.
|
||||
fn name(&self) -> &str;
|
||||
|
||||
/// Gibt das Skill-Format zurück das dieser Provider unterstützt.
|
||||
/// Standard: Xml — für lokale Modelle.
|
||||
fn skill_format(&self) -> SkillFormat {
|
||||
SkillFormat::Xml
|
||||
}
|
||||
|
||||
@@ -1,53 +1,56 @@
|
||||
// nazarick-core/src/llm/types.rs
|
||||
//
|
||||
// Gemeinsame Datentypen für alle LLM-Provider.
|
||||
// Jeder Provider (LmStudio, Ollama, Mistral) nutzt diese Typen.
|
||||
|
||||
/// Repräsentiert eine einzelne Nachricht in einem Gespräch.
|
||||
/// Entspricht dem Message-Format das alle gängigen LLM APIs verwenden.
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Message {
|
||||
/// Rolle des Absenders: "system", "user" oder "assistant"
|
||||
pub role: String,
|
||||
/// Inhalt der Nachricht
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
/// Erstellt eine System-Nachricht (z.B. den Persönlichkeits-Prompt)
|
||||
pub fn system(content: impl Into<String>) -> Self {
|
||||
Self { role: "system".to_string(), content: content.into() }
|
||||
}
|
||||
|
||||
/// Erstellt eine User-Nachricht
|
||||
pub fn user(content: impl Into<String>) -> Self {
|
||||
Self { role: "user".to_string(), content: content.into() }
|
||||
}
|
||||
|
||||
/// Erstellt eine Assistant-Nachricht (vorherige Antworten für Kontext)
|
||||
pub fn assistant(content: impl Into<String>) -> Self {
|
||||
Self { role: "assistant".to_string(), content: content.into() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Konfiguration für einen einzelnen LLM-Aufruf.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LlmRequest {
|
||||
/// Der vollständige Gesprächsverlauf inklusive System-Prompt
|
||||
pub messages: Vec<Message>,
|
||||
/// Maximale Anzahl Token in der Antwort
|
||||
pub max_tokens: u32,
|
||||
/// Kreativität der Antwort (0.0 = deterministisch, 1.0 = sehr kreativ)
|
||||
pub temperature: f32,
|
||||
pub tools: Option<Vec<serde_json::Value>>,
|
||||
}
|
||||
|
||||
impl LlmRequest {
|
||||
pub fn simple(messages: Vec<Message>, max_tokens: u32, temperature: f32) -> Self {
|
||||
Self { messages, max_tokens, temperature, tools: None }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ToolCall {
|
||||
pub id: Option<String>,
|
||||
pub function: ToolCallFunction,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ToolCallFunction {
|
||||
pub name: String,
|
||||
pub arguments: String,
|
||||
}
|
||||
|
||||
/// Antwort eines LLM-Aufrufs.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LlmResponse {
|
||||
/// Der generierte Text
|
||||
pub content: String,
|
||||
/// Anzahl der Input-Token (für Usage-Tracking)
|
||||
pub tokens_input: u64,
|
||||
/// Anzahl der Output-Token (für Usage-Tracking)
|
||||
pub tokens_output: u64,
|
||||
pub tool_calls: Option<Vec<ToolCall>>,
|
||||
pub cost: Option<f64>,
|
||||
}
|
||||
Reference in New Issue
Block a user