mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-05 22:12:12 +00:00
* FEATURE: add inferred concepts system This commit adds a new inferred concepts system that: - Creates a model for storing concept labels that can be applied to topics - Provides AI personas for finding new concepts and matching existing ones - Adds jobs for generating concepts from popular topics - Includes a scheduled job that automatically processes engaging topics * FEATURE: Extend inferred concepts to include posts * Adds support for concepts to be inferred from and applied to posts * Replaces daily task with one that handles both topics and posts * Adds database migration for posts_inferred_concepts join table * Updates PersonaContext to include inferred concepts Co-authored-by: Roman Rizzi <rizziromanalejandro@gmail.com> Co-authored-by: Keegan George <kgeorge13@gmail.com>
132 lines
3.8 KiB
Ruby
132 lines
3.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe DiscourseAi::Completions::StructuredOutput do
|
|
subject(:structured_output) do
|
|
described_class.new(
|
|
{
|
|
message: {
|
|
type: "string",
|
|
},
|
|
bool: {
|
|
type: "boolean",
|
|
},
|
|
number: {
|
|
type: "integer",
|
|
},
|
|
status: {
|
|
type: "string",
|
|
},
|
|
list: {
|
|
type: "array",
|
|
items: {
|
|
type: "string",
|
|
},
|
|
},
|
|
},
|
|
)
|
|
end
|
|
|
|
describe "Parsing structured output on the fly" do
|
|
it "acts as a buffer for an streamed JSON" do
|
|
chunks = [
|
|
+"{\"message\": \"Line 1\\n",
|
|
+"Line 2\\n",
|
|
+"Line 3\", ",
|
|
+"\"bool\": true,",
|
|
+"\"number\": 4",
|
|
+"2,",
|
|
+"\"status\": \"o",
|
|
+"\\\"k\\\"\"}",
|
|
]
|
|
|
|
structured_output << chunks[0]
|
|
expect(structured_output.read_buffered_property(:message)).to eq("Line 1\n")
|
|
|
|
structured_output << chunks[1]
|
|
expect(structured_output.read_buffered_property(:message)).to eq("Line 2\n")
|
|
|
|
structured_output << chunks[2]
|
|
expect(structured_output.read_buffered_property(:message)).to eq("Line 3")
|
|
|
|
structured_output << chunks[3]
|
|
expect(structured_output.read_buffered_property(:bool)).to eq(true)
|
|
|
|
# Waiting for number to be fully buffered.
|
|
structured_output << chunks[4]
|
|
expect(structured_output.read_buffered_property(:bool)).to eq(true)
|
|
expect(structured_output.read_buffered_property(:number)).to be_nil
|
|
|
|
structured_output << chunks[5]
|
|
expect(structured_output.read_buffered_property(:number)).to eq(42)
|
|
|
|
structured_output << chunks[6]
|
|
expect(structured_output.read_buffered_property(:number)).to eq(42)
|
|
expect(structured_output.read_buffered_property(:bool)).to eq(true)
|
|
expect(structured_output.read_buffered_property(:status)).to eq("o")
|
|
|
|
structured_output << chunks[7]
|
|
expect(structured_output.read_buffered_property(:status)).to eq("\"k\"")
|
|
|
|
# No partial string left to read.
|
|
expect(structured_output.read_buffered_property(:status)).to eq("")
|
|
end
|
|
|
|
it "supports array types" do
|
|
chunks = [
|
|
+"{ \"",
|
|
+"list",
|
|
+"\":",
|
|
+" [\"",
|
|
+"Hello!",
|
|
+" I am",
|
|
+" a ",
|
|
+"chunk\",",
|
|
+"\"There\"",
|
|
+"]}",
|
|
]
|
|
|
|
structured_output << chunks[0]
|
|
structured_output << chunks[1]
|
|
structured_output << chunks[2]
|
|
expect(structured_output.read_buffered_property(:list)).to eq(nil)
|
|
|
|
structured_output << chunks[3]
|
|
expect(structured_output.read_buffered_property(:list)).to eq([""])
|
|
|
|
structured_output << chunks[4]
|
|
expect(structured_output.read_buffered_property(:list)).to eq(["Hello!"])
|
|
|
|
structured_output << chunks[5]
|
|
structured_output << chunks[6]
|
|
structured_output << chunks[7]
|
|
|
|
expect(structured_output.read_buffered_property(:list)).to eq(["Hello! I am a chunk"])
|
|
|
|
structured_output << chunks[8]
|
|
expect(structured_output.read_buffered_property(:list)).to eq(
|
|
["Hello! I am a chunk", "There"],
|
|
)
|
|
|
|
structured_output << chunks[9]
|
|
expect(structured_output.read_buffered_property(:list)).to eq(
|
|
["Hello! I am a chunk", "There"],
|
|
)
|
|
end
|
|
end
|
|
|
|
describe "dealing with non-JSON responses" do
|
|
it "treat it as plain text once we determined it's invalid JSON" do
|
|
chunks = [+"I'm not", +"a", +"JSON :)"]
|
|
|
|
structured_output << chunks[0]
|
|
expect(structured_output.read_buffered_property(nil)).to eq("I'm not")
|
|
|
|
structured_output << chunks[1]
|
|
expect(structured_output.read_buffered_property(nil)).to eq("a")
|
|
|
|
structured_output << chunks[2]
|
|
expect(structured_output.read_buffered_property(nil)).to eq("JSON :)")
|
|
end
|
|
end
|
|
end
|