Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ We just finished a full course on retrieval-augmented generation and built a sma

## Try it

You need Python 3.12+, [uv](https://docs.astral.sh/uv/) and [Ollama](https://ollama.com/).
**On Windows, without installing anything else:** download Open RAG, the desktop app, from the
[latest release](https://github.com/Carthavate/open-rag-engine/releases/latest). Put the `.exe` and its `.bin` parts in
one folder and run the `.exe`. It brings its own models and works offline.

**From source:** you need Python 3.12+, [uv](https://docs.astral.sh/uv/) and [Ollama](https://ollama.com/).

```bash
uv sync
Expand Down Expand Up @@ -88,5 +92,7 @@ We review each other's work through pull requests. See [CONTRIBUTING.md](CONTRIB

## Team

- [Wadi](https://github.com/TWadi)
- [Ghassen](https://github.com/GhassenJamoussi99)
Open RAG is developed by:

- Wadi Touil: [GitHub](https://github.com/TWadi) · [LinkedIn](https://www.linkedin.com/in/wadi-touil-117472319/)
- Ghassen Jamoussi: [GitHub](https://github.com/GhassenJamoussi99) · [LinkedIn](https://www.linkedin.com/in/ghassen-jamoussi/)
5 changes: 5 additions & 0 deletions desktop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ npm test

- **Against a real engine in the browser:** start `ORE_API_TOKEN=<32+ chars> uv run ore serve --origin http://localhost:5176`, then open `http://localhost:5176/?api=http://127.0.0.1:<port>&token=<token>`.
- **Live reload inside the app (Debug builds only):** set `OPENRAG_DEV_URL=http://localhost:5176` before starting `OpenRag.exe`.

## Installer

`installer/build.py` builds `OpenRag-Setup.exe`, a self-contained setup that brings its own Python, models and
llama.cpp server, so it needs no internet after installing. See [installer/README.md](../installer/README.md).
93 changes: 74 additions & 19 deletions desktop/src/OpenRag.App/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public partial class MainWindow : Window
private const string DevUrlVariable = "OPENRAG_DEV_URL";

private static readonly TimeSpan EngineStartTimeout = TimeSpan.FromMinutes(3);
private static readonly TimeSpan HealthCheckTimeout = TimeSpan.FromSeconds(2);
private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web);
private static readonly string WebRoot = Path.Combine(AppContext.BaseDirectory, "wwwroot");

Expand All @@ -32,7 +33,8 @@ public partial class MainWindow : Window
private readonly HttpClient _http = new(new SocketsHttpHandler { UseProxy = false }) { Timeout = Timeout.InfiniteTimeSpan };
private readonly SemaphoreSlim _lifecycle = new(1, 1);
private readonly Dictionary<string, CancellationTokenSource> _asks = [];
private EngineHost? _engine;
private ChildHost? _engine;
private ChildHost? _llm;
private EngineProxy? _proxy;
private object _engineState = new { type = "engine", state = "starting" };
private string _origin = $"https://{AppHost}";
Expand Down Expand Up @@ -115,44 +117,77 @@ private async Task LaunchEngineAsync()
}
try
{
var engine = new EngineHost(command, command.ServeArguments(_paths.Data, Environment.ProcessId), _paths.EngineLog);
engine.Started += process => _job?.Add(process);
// Only the current engine may report a crash; a late exit from a replaced one is ignored.
engine.Exited += code => Dispatcher.BeginInvoke(() =>
var environment = new Dictionary<string, string> { ["ORE_API_TOKEN"] = _token };
string? llmUrl = null;
Task llmReady = Task.CompletedTask;
if (command.Llm is { } bundle)
{
if (!ReferenceEquals(engine, _engine))
{
return;
}
_proxy = null;
SetEngineState("failed", $"The engine stopped unexpectedly (exit code {code}). Details are in {_paths.EngineLog}.");
});
_engine = engine;
var connection = await engine.StartAsync(new Dictionary<string, string> { ["ORE_API_TOKEN"] = _token }, EngineStartTimeout);
// The language model loads while the engine warms up; both must be ready before the first question.
var port = LlmBundle.FreePort();
var key = Token.New();
llmUrl = $"http://127.0.0.1:{port}/v1";
environment["ORE_LLM_API_KEY"] = key;
_llm = Watch(new ChildHost(new EngineCommand(bundle.ServerPath, [], Path.GetDirectoryName(bundle.ServerPath)!, null),
bundle.Arguments(port), _paths.LlmLog, "language model"), _paths.LlmLog);
llmReady = _llm.StartAsync(new Dictionary<string, string> { [LlmBundle.ApiKeyVariable] = key },
cancel => IsHealthyAsync(port, cancel), EngineStartTimeout);
}
_engine = Watch(new ChildHost(command, command.ServeArguments(_paths.Data, Environment.ProcessId, llmUrl), _paths.EngineLog),
_paths.EngineLog);
var connection = await _engine.StartAsync(environment, EngineStartTimeout);
await llmReady;
_proxy = new EngineProxy(_http, connection.BaseUrl, _token);
SetEngineState("ready");
}
catch (EngineStartException e)
{
SetEngineState("failed", $"{e.Message}\n\nThe full log is in {_paths.EngineLog}.");
await StopEngineAsync();
SetEngineState("failed", $"{e.Message}\n\nThe logs are in {_paths.Logs}.");
}
catch (Exception e)
{
// Anything else (no permission to create the log folder, a blocked program) must not leave "starting" forever.
LogError(e);
await StopEngineAsync();
SetEngineState("failed", $"Open RAG couldn't start its engine: {e.Message}\n\nDetails are in {_paths.ErrorLog}.");
}
}

/// <summary>Puts a helper process in the job object and reports a crash, but only while it is still the current one:
/// a late exit from a replaced engine or model is ignored.</summary>
private ChildHost Watch(ChildHost host, string log)
{
host.Started += process => _job?.Add(process);
host.Exited += code => Dispatcher.BeginInvoke(() =>
{
if (!ReferenceEquals(host, _engine) && !ReferenceEquals(host, _llm))
{
return;
}
_proxy = null;
var what = ReferenceEquals(host, _llm) ? "language model" : "engine";
SetEngineState("failed", $"The {what} stopped unexpectedly (exit code {code}). Details are in {log}.");
});
return host;
}

/// <summary>llama-server answers /health with 200 once the model is loaded (503 while loading).</summary>
private async Task<bool> IsHealthyAsync(int port, CancellationToken cancel)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancel);
timeout.CancelAfter(HealthCheckTimeout);
using var response = await _http.GetAsync($"http://127.0.0.1:{port}/health", timeout.Token);
return response.IsSuccessStatusCode;
}

private async Task StopEngineAsync()
{
_proxy = null;
var engine = _engine;
var llm = _llm;
_engine = null;
if (engine is not null)
{
await engine.DisposeAsync();
}
_llm = null;
await Task.WhenAll(engine?.DisposeAsync().AsTask() ?? Task.CompletedTask, llm?.DisposeAsync().AsTask() ?? Task.CompletedTask);
}

private void LogError(Exception e)
Expand Down Expand Up @@ -262,6 +297,9 @@ private async void OnWebMessage(object? sender, CoreWebView2WebMessageReceivedEv
case "restartEngine":
await StartEngineAsync(restart: true);
break;
case "openLink":
OpenLink(message.GetProperty("url").GetString());
break;
case "api":
await ForwardAsync(message);
break;
Expand Down Expand Up @@ -420,6 +458,23 @@ private async Task OpenSourceAsync(string? path, bool reveal)
}
}

/// <summary>Opens one of the app's own links (the credits, the source code) in the default browser.</summary>
private void OpenLink(string? url)
{
if (!ExternalLinks.IsAllowed(url))
{
return;
}
try
{
Process.Start(new ProcessStartInfo(url!) { UseShellExecute = true });
}
catch (System.ComponentModel.Win32Exception ex)
{
Post(new { type = "notice", message = $"Couldn't open your browser: {ex.Message}" });
}
}

private async Task ShutdownAsync()
{
SystemEvents.UserPreferenceChanged -= OnUserPreferenceChanged;
Expand Down
3 changes: 3 additions & 0 deletions desktop/src/OpenRag.App/OpenRag.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<AssemblyName>OpenRag</AssemblyName>
<AssemblyTitle>Open RAG</AssemblyTitle>
<Product>Open RAG</Product>
<Authors>Wadi Touil; Ghassen Jamoussi</Authors>
<Company>Carthavate</Company>
<Copyright>Copyright (C) 2026 Wadi Touil and Ghassen Jamoussi</Copyright>
<Version>0.1.0</Version>
<ApplicationIcon>Assets\OpenRag.ico</ApplicationIcon>
<WebRoot>$(MSBuildThisFileDirectory)..\..\web\</WebRoot>
Expand Down
Loading
Loading