SciSharp / LLamaSharp

A C#/.NET library to run LLM (🦙LLaMA/LLaVA) on your local device efficiently.
https://scisharp.github.io/LLamaSharp
MIT License
2.56k stars 334 forks source link

[Feature]: Support JSON Schema from llama.cpp #798

Open m0nsky opened 3 months ago

m0nsky commented 3 months ago

Background & Description

I was working on a C# class -> GBNF converter, and came across this:

https://github.com/ggerganov/llama.cpp/blob/master/grammars/README.md#json-schemas--gbnf

Here's an example, thanks to this reddit post.

Example JSON:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "reply": {"type": "string"},
    "mood": {"type": "string"},
    "intent": {"type": "string"}
  },
  "required": ["reply", "mood", "intent"]
}

Example output:

 User: I gave you a cookie.

Please respond in JSON format with your reply, mood, and intent:
{"reply": "Thank you for the cookie! It's delicious!", "mood": "Happy", "intent": "Express gratitude" }

API & Usage

No response

How to implement

No response

martindevans commented 3 months ago

Related: https://github.com/SciSharp/LLamaSharp/issues/309 ?

m0nsky commented 3 months ago

Yes somewhat, that PR sparked my initial interest in developing the C# class -> GBNF converter. However, the JSON Schema functionality seems to be already implemented on main & server on the llama.cpp side by passing the -j parameter, so it wouldn't make sense to reinvent the wheel.

RobertRappe commented 3 months ago

As someone slowly working on my own C# agentic system and has been exploring using JSON to format communication with the LLM, getting access to the llama.cpp JSON and grammar tools through LLamaSharp would be really useful.👍