discourse-ai/lib/discourse_ai/completions/endpoints/fake.rb

152 lines
4.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module DiscourseAi
module Completions
module Endpoints
class Fake < Base
class << self
def can_contact?(_endpoint_name, model_name)
model_name == "fake"
end
def correctly_configured?(_model_name)
true
end
def endpoint_name(_model_name)
"Test - fake model"
end
end
STOCK_CONTENT = <<~TEXT
# Discourse Markdown Styles Showcase
Welcome to the **Discourse Markdown Styles Showcase**! This _post_ is designed to demonstrate a wide range of Markdown capabilities available in Discourse.
## Lists and Emphasis
- **Bold Text**: To emphasize a point, you can use bold text.
- _Italic Text_: To subtly highlight text, italics are perfect.
- ~~Strikethrough~~: Sometimes, marking text as obsolete requires a strikethrough.
> **Note**: Combining these _styles_ can **_really_** make your text stand out!
1. First item
2. Second item
* Nested bullet
* Another nested bullet
3. Third item
## Links and Images
You can easily add [links](https://meta.discourse.org) to your posts. For adding images, use this syntax:
![Discourse Logo](https://meta.discourse.org/images/discourse-logo.svg)
## Code and Quotes
Inline `code` is used for mentioning small code snippets like `let x = 10;`. For larger blocks of code, fenced code blocks are used:
```javascript
function greet() {
console.log("Hello, Discourse Community!");
}
greet();
```
> Blockquotes can be very effective for highlighting user comments or important sections from cited sources. They stand out visually and offer great readability.
## Tables and Horizontal Rules
Creating tables in Markdown is straightforward:
| Header 1 | Header 2 | Header 3 |
| ---------|:--------:| --------:|
| Row 1, Col 1 | Centered | Right-aligned |
| Row 2, Col 1 | **Bold** | _Italic_ |
| Row 3, Col 1 | `Inline Code` | [Link](https://meta.discourse.org) |
To separate content sections:
---
## Final Thoughts
Congratulations, you've now seen a small sample of what Discourse's Markdown can do! For more intricate formatting, consider exploring the advanced styling options. Remember that the key to great formatting is not just the available tools, but also the **clarity** and **readability** it brings to your readers.
TEXT
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596) This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments. Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer. EG: - User: how many cars are there in europe - Model: detailed answer about cars in europe including the term car and vehicle many times - User: Nice, what about trains are there in the US In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal. Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content. The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui. This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation. Key changes: 1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation. 2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt. 3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question. 4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration. 5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality. This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
def self.with_fake_content(content)
@fake_content = content
yield
ensure
@fake_content = nil
end
def self.fake_content
@fake_content || STOCK_CONTENT
end
def self.delays
@delays ||= Array.new(10) { rand * 6 }
end
def self.delays=(delays)
@delays = delays
end
def self.chunk_count
@chunk_count ||= 10
end
def self.chunk_count=(chunk_count)
@chunk_count = chunk_count
end
def self.last_call
@last_call
end
def self.last_call=(params)
@last_call = params
end
def perform_completion!(dialect, user, model_params = {})
self.class.last_call = { dialect: dialect, user: user, model_params: model_params }
content = self.class.fake_content
if block_given?
split_indices = (1...content.length).to_a.sample(self.class.chunk_count - 1).sort
indexes = [0, *split_indices, content.length]
original_content = content
content = +""
cancel = false
cancel_proc = -> { cancel = true }
i = 0
indexes
.each_cons(2)
.map { |start, finish| original_content[start...finish] }
.each do |chunk|
break if cancel
if self.class.delays.present? &&
(delay = self.class.delays[i % self.class.delays.length])
sleep(delay)
i += 1
end
break if cancel
content << chunk
yield(chunk, cancel_proc)
end
end
content
end
end
end
end
end