mirror of
https://github.com/Alvin-Zilverstand/school.git
synced 2026-11-30 18:17:51 +01:00
f
This commit is contained in:
1 parent
f3e79a7aeb
commit
d420c317ab
88 files changed
+36601
No files matched your search
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.5.1" />
|
||||
<PackageReference Include="PdfPig" Version="0.1.14" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,124 @@
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using UglyToad.PdfPig;
|
||||
using UglyToad.PdfPig.Content;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
|
||||
|
||||
static string ExtractText(string filePath)
|
||||
{
|
||||
var ext = Path.GetExtension(filePath).ToLowerInvariant();
|
||||
return ext switch
|
||||
{
|
||||
".pdf" => ExtractFromPdf(filePath),
|
||||
".docx" => ExtractFromDocx(filePath),
|
||||
_ => throw new NotSupportedException($"Unsupported file type: {ext}")
|
||||
};
|
||||
}
|
||||
|
||||
static string ExtractFromPdf(string filePath)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
using var document = PdfDocument.Open(filePath);
|
||||
foreach (Page page in document.GetPages())
|
||||
sb.AppendLine(page.Text);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
static string ExtractFromDocx(string filePath)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
using var doc = WordprocessingDocument.Open(filePath, false);
|
||||
var body = doc.MainDocumentPart?.Document?.Body;
|
||||
if (body is null) return string.Empty;
|
||||
foreach (var para in body.Descendants<Paragraph>())
|
||||
sb.AppendLine(para.InnerText);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
||||
var inputFile = args.Length > 0 ? args[0] : throw new Exception("Pass a file path as argument.");
|
||||
var text = ExtractText(inputFile);
|
||||
|
||||
var client = new HttpClient();
|
||||
client.Timeout = TimeSpan.FromMinutes(10);
|
||||
|
||||
var url = "http://192.168.178.214:11434/api/generate";
|
||||
|
||||
var body = new
|
||||
{
|
||||
model = "qwen3-vl:8b-instruct",
|
||||
prompt = $@"Extract order data from the document below and return it as JSON.
|
||||
|
||||
CRITICAL RULES — read before extracting:
|
||||
- ONLY use values that are explicitly written in the document
|
||||
- If a field is not present in the document, use """" for strings or 0 for numbers
|
||||
- NEVER invent, guess, or infer values that are not clearly stated
|
||||
- NEVER generate product codes, reference numbers, or relation codes unless they appear word-for-word in the document
|
||||
- Referentie = PO Number if present, otherwise """"
|
||||
- Relatiecode = customer/relation code if explicitly labeled as such, otherwise """"
|
||||
- productcode = numeric code explicitly labeled as a product/article code, otherwise 0
|
||||
- ONLY return the json data, no other explenations or other text than the json
|
||||
|
||||
Output ONLY this JSON structure:
|
||||
[{{
|
||||
""order"": {{
|
||||
""Naam"": ""<customer name from document or empty>"",
|
||||
""Datum"": ""<date in yyyy-MM-dd or empty>"",
|
||||
""Relatiecode"": ""<relation code if explicitly present, else empty>"",
|
||||
""Referentie"": ""<PO number if explicitly present, else empty>"",
|
||||
""regels"": [
|
||||
{{
|
||||
""productcode"": <integer code if explicitly present, else 0>,
|
||||
""omschrijving"": ""<product description>"",
|
||||
""aantal"": <quantity as integer>,
|
||||
""prijs"": <unit price as decimal>
|
||||
}}
|
||||
]
|
||||
}}
|
||||
}}]
|
||||
|
||||
Document:
|
||||
{text}
|
||||
",
|
||||
system = @"You are a strict document-to-JSON data extractor for order processing.
|
||||
|
||||
Your job is EXTRACTION ONLY — not inference, not completion, not enrichment.
|
||||
|
||||
You ONLY output valid JSON matching the exact schema provided.
|
||||
You NEVER output explanations, markdown, code blocks, or comments.
|
||||
You NEVER add fields not in the schema.
|
||||
|
||||
Anti-hallucination rules:
|
||||
- If you are not 100% certain a value appears in the document, leave it empty or 0
|
||||
- Product codes, reference numbers, and relation codes are NEVER made up — they must appear verbatim in the document
|
||||
- Do not infer a product code from a product name or description
|
||||
- Do not infer a relation code from a customer name
|
||||
- Do not infer a reference from an invoice number unless explicitly labeled as PO or reference
|
||||
- When in doubt, use empty string or 0
|
||||
|
||||
Data type rules:
|
||||
- Datum must be yyyy-MM-dd format
|
||||
- productcode and aantal must be integers
|
||||
- prijs must be a decimal number
|
||||
- All string values use double quotes
|
||||
- No trailing commas
|
||||
|
||||
Your entire response must be JSON and nothing else.",
|
||||
keep_alive = "24h",
|
||||
stream = false
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(body);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await client.PostAsync(url, content);
|
||||
var result = await response.Content.ReadAsStringAsync();
|
||||
|
||||
var parsed = JsonSerializer.Deserialize<OllamaResponse>(result);
|
||||
Console.WriteLine(parsed?.Response);
|
||||
|
||||
record OllamaResponse([property: JsonPropertyName("response")] string Response);
|
||||
Reference in new issue
Block a user