Skip to content

Commit 0efec75

Browse files
Add first files
0 parents  commit 0efec75

File tree

6 files changed

+282
-0
lines changed

6 files changed

+282
-0
lines changed

Demo/Test.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using UnityEngine;
2+
using HardCodeDev.SimpleOllamaUnity;
3+
4+
namespace HardCodeDev.Examples
5+
{
6+
public class Test : MonoBehaviour
7+
{
8+
private async void Start()
9+
{
10+
var ollama = new Ollama(new OllamaConfig(
11+
modelName: "qwen2.5:3b",
12+
systemPrompt: "Your answer mustn't be more than 10 words"
13+
));
14+
15+
var response = await ollama.SendMessage(new OllamaRequest(
16+
userPrompt: "When Github was created?"
17+
));
18+
19+
Debug.Log(response);
20+
}
21+
}
22+
}

Demo/Test.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 HardCodeDev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
![Ollama](https://img.shields.io/badge/Ollama-%23000000?logo=Ollama)
2+
![Unity](https://img.shields.io/badge/Unity-unity?logo=Unity&color=%23000000)
3+
![C#](https://img.shields.io/badge/C%23-%23512BD4?logo=.NET)
4+
![License](https://img.shields.io/github/license/HardCodeDev777/SimpleOllamaUnity?color=%2305991d)
5+
![Last commit](https://img.shields.io/github/last-commit/HardCodeDev777/SimpleOllamaUnity?color=%2305991d)
6+
![Tag](https://img.shields.io/github/v/tag/HardCodeDev777/SimpleOllamaUnity)
7+
![Top lang](https://img.shields.io/github/languages/top/HardCodeDev777/SimpleOllamaUnity)
8+
9+
# 🦙 SimpleOllamaUnity — Unity Editor Tool
10+
11+
> Communicate with local LLMs in Unity using [Ollama](https://ollama.com) — in just **two lines of code**.
12+
13+
---
14+
15+
## 🚀 Overview
16+
17+
**SimpleOllamaUnity** is an Unity extension that lets you communicate with Ollama in just two lines of code!
18+
It also works in runtime, so you can use it in your games!
19+
20+
You can easily configure the following for a quick start:
21+
- 🤖 Model
22+
- 📃 System prompt
23+
- 🌐 Ollama URI
24+
- 👀 Reasoning (optional — can be disabled)
25+
26+
---
27+
28+
## 📦 Installation
29+
30+
1. Download the latest .unitypackage file from the Releases page.
31+
2. Drag & drop it into your Unity project window.
32+
3. Unity will automatically compile the editor extension.
33+
34+
No additional setup required.
35+
36+
The Plugins folder includes several .dll files required for integration.
37+
38+
---
39+
40+
## 💻 Usage
41+
42+
```csharp
43+
var ollama = new Ollama(new OllamaConfig(
44+
modelName: "qwen2.5:3b",
45+
systemPrompt: "Your answer mustn't be more than 10 words"
46+
));
47+
48+
var response = await ollama.SendMessage(new OllamaRequest(
49+
userPrompt: "When was GitHub created?"
50+
));
51+
```
52+
53+
Yes, that’s it — only **two lines of code**! 🎉
54+
55+
 
56+
57+
To use a custom server URI:
58+
59+
```csharp
60+
var ollama = new Ollama(new OllamaConfig(
61+
modelName: "qwen2.5:3b",
62+
systemPrompt: "Your answer mustn't be more than 10 words",
63+
uri: "http://my-custom-server.local:3000/api/process"
64+
));
65+
```
66+
67+
 
68+
69+
You can also remove reasoning from models that can do it:
70+
71+
```csharp
72+
var response = await ollama.SendMessage(new OllamaRequest(
73+
userPrompt: "When was GitHub created?",
74+
clearThinking: true
75+
));
76+
```
77+
78+
This will remove all reasoning (from `<think>` to `</think>`).
79+
80+
&nbsp;
81+
&nbsp;
82+
83+
### 🧪 Full Example:
84+
85+
```csharp
86+
using UnityEngine;
87+
using HardCodeDev.SimpleOllamaUnity;
88+
89+
public class Test : MonoBehaviour
90+
{
91+
private async void Start()
92+
{
93+
var ollama = new OllamaBase(new OllamaConfig(
94+
modelName: "qwen2.5:3b",
95+
systemPrompt: "Your answer mustn't be more than 10 words"
96+
));
97+
98+
var response = await ollama.SendMessage(new OllamaRequest(
99+
userPrompt: "When was GitHub created?"
100+
));
101+
102+
Debug.Log(response); // Prints LLM response to the console
103+
}
104+
}
105+
```
106+
107+
---
108+
109+
## 🛠 TODO
110+
111+
- [ ] Review which `.dll` files in the `Plugins` folder are actually required and remove the unnecessary ones.
112+
113+
---
114+
115+
## 📄 License
116+
117+
This project is licensed under the **MIT License**.
118+
See the [LICENSE](LICENSE) file for full terms.
119+
120+
---
121+
122+
## 👨‍💻 Author
123+
124+
**HardCodeDev**
125+
- [GitHub](https://github.com/HardCodeDev777)
126+
- [Itch.io](https://hardcodedev.itch.io/)
127+
128+
---
129+
130+
> 💬 Got feedback, found a bug, or want to contribute? Open an issue or fork the repo on GitHub!

src/Ollama.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using UnityEngine;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.AI;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
8+
namespace HardCodeDev.SimpleOllamaUnity
9+
{
10+
/// <summary>
11+
/// Structure with Ollama settrings
12+
/// </summary>
13+
[System.Serializable]
14+
public struct OllamaConfig
15+
{
16+
public string modelName, uri, systemPrompt;
17+
18+
public OllamaConfig(string modelName, string systemPrompt, string uri = "http://localhost:11434")
19+
{
20+
this.modelName = modelName;
21+
this.uri = uri;
22+
this.systemPrompt = systemPrompt;
23+
}
24+
}
25+
26+
/// <summary>
27+
/// Structure with request settrings
28+
/// </summary>
29+
[System.Serializable]
30+
public struct OllamaRequest
31+
{
32+
public string userPrompt;
33+
public bool clearThinking;
34+
35+
public OllamaRequest(string userPrompt, bool clearThinking = false)
36+
{
37+
this.userPrompt = userPrompt;
38+
this.clearThinking = clearThinking;
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Base class for communicating with Ollama
44+
/// </summary>
45+
public class Ollama : MonoBehaviour
46+
{
47+
private List<ChatMessage> _chatHistory = new();
48+
private IChatClient _chatClient;
49+
50+
/// <summary>
51+
/// Constructor to initiliaze Ollama
52+
/// </summary>
53+
/// <param name="config">Ollama settings</param>
54+
public Ollama(OllamaConfig config) => InitOllama(config);
55+
56+
private void InitOllama(OllamaConfig config)
57+
{
58+
var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
59+
{ DisableDefaults = true });
60+
61+
builder.Services.AddChatClient(new OllamaChatClient(new System.Uri(config.uri), config.modelName));
62+
63+
_chatClient = builder.Build().Services.GetRequiredService<IChatClient>();
64+
65+
_chatHistory.Add(new(ChatRole.System, config.systemPrompt));
66+
}
67+
68+
/// <summary>
69+
/// Method to send and get messages from LLM via Ollama
70+
/// </summary>
71+
/// <param name="request">Request settings</param>
72+
/// <returns>LLM's reponse</returns>
73+
public async Task<string> SendMessage(OllamaRequest request)
74+
{
75+
_chatHistory.Add(new(ChatRole.User, request.userPrompt));
76+
77+
var chatResponse = "";
78+
await foreach (var item in _chatClient.GetStreamingResponseAsync(_chatHistory)) chatResponse += item.Text;
79+
80+
_chatHistory.Add(new(ChatRole.Assistant, chatResponse));
81+
82+
if (request.clearThinking == true)
83+
{
84+
var newChatResponse = ClearThinking(chatResponse);
85+
return newChatResponse;
86+
}
87+
else return chatResponse;
88+
}
89+
90+
private string ClearThinking(string input)
91+
{
92+
try
93+
{
94+
var start = input.IndexOf("<think>");
95+
var end = input.IndexOf("</think>");
96+
return input.Remove(start, (end + "</think>".Length) - start);
97+
}
98+
catch
99+
{
100+
Debug.LogError("No <think> were found!");
101+
return "";
102+
}
103+
}
104+
}
105+
}

src/Ollama.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)