Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-10-06 6bac54b555d27f1a85c1e4f011a6e7bd9111b0d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*-------------------------------------------------------------------------
 *
 * tokenizer.c
 *      Text tokenization for chunking
 *
 * This file provides tokenization functionality for text chunking.
 *
 * CURRENT IMPLEMENTATION: Uses character-based approximation (4 chars ≈ 1 token)
 * FUTURE: Integrate tiktoken library for accurate tokenization
 *
 * To integrate tiktoken:
 * 1. Add tiktoken library to build (see https://github.com/openai/tiktoken)
 * 2. Link against tiktoken
 * 3. Replace approximation functions with tiktoken calls
 * 4. Use cl100k_base encoding for OpenAI models
 *
 * Copyright (c) 2025 - 2026, pgEdge, Inc.
 *
 *-------------------------------------------------------------------------
 */
#include "vectorizer/pgedge_vectorizer.h"
 
/*
 * Approximate token count
 *
 * This is a simple approximation: ~4 characters per token for English text.
 * This is reasonably accurate for most use cases but not perfect.
 *
 * For production use with OpenAI models, consider integrating tiktoken.
 */
int
count_tokens(const char *text, const char *model)
{
    int char_count = 0;
    int token_estimate;
    const char *p;
 
    if (text == NULL || text[0] == '\0')
        return 0;
 
    /* Count characters (UTF-8 aware) */
    for (p = text; *p; p++)
    {
        /* Only count the start of each UTF-8 character */
        if ((*p & 0xC0) != 0x80)
            char_count++;
    }
 
    /* Estimate tokens (4 chars per token is a reasonable approximation) */
    token_estimate = (char_count + 3) / 4;
 
    elog(DEBUG2, "Token count estimate: %d (from %d characters)",
         token_estimate, char_count);
 
    return token_estimate;
}
 
/*
 * Tokenize text into token IDs
 *
 * This is a placeholder for tiktoken integration.
 * Currently returns NULL as we don't need actual token IDs for chunking.
 *
 * When integrating tiktoken:
 * - Load the appropriate encoding (e.g., cl100k_base)
 * - Call tiktoken's encode function
 * - Return array of token IDs
 */
int *
tokenize_text(const char *text, const char *model, int *token_count)
{
    /* For now, we don't need actual tokenization, just counting */
    *token_count = count_tokens(text, model);
 
    elog(DEBUG1, "Tokenization not fully implemented - using approximation");
    return NULL;
}
 
/*
 * Detokenize token IDs back to text
 *
 * This is a placeholder for tiktoken integration.
 *
 * When integrating tiktoken:
 * - Call tiktoken's decode function
 * - Return the decoded text
 */
char *
detokenize_tokens(const int *tokens, int token_count, const char *model)
{
    /* Placeholder */
    elog(DEBUG1, "Detokenization not implemented");
    return NULL;
}
 
/*
 * Get character offset for a given token count
 *
 * This function estimates where in the text a certain number of tokens
 * would end. Used for chunking.
 */
int
get_char_offset_for_tokens(const char *text, int target_tokens, const char *model)
{
    int estimated_chars;
    const char *p;
    int actual_chars = 0;
 
    if (text == NULL || target_tokens <= 0)
        return 0;
 
    /* Estimate character position (4 chars per token) */
    estimated_chars = target_tokens * 4;
 
    /* Count actual characters (UTF-8 aware) */
    for (p = text; *p && actual_chars < estimated_chars; p++)
    {
        /* Only count the start of each UTF-8 character */
        if ((*p & 0xC0) != 0x80)
            actual_chars++;
    }
 
    /* Return byte offset */
    return p - text;
}
 
/*
 * Find a good break point near the target position
 *
 * Tries to break at sentence or paragraph boundaries if possible.
 */
int
find_good_break_point(const char *text, int target_offset, int max_offset)
{
    int best_offset = target_offset;
    int search_window = 50;  /* Look 50 chars before and after */
    int search_start;
 
    if (target_offset >= max_offset)
        return max_offset;
 
    /* Calculate search start, preventing negative index */
    search_start = target_offset - search_window;
    if (search_start < 0)
        search_start = 0;
 
    /* Look for paragraph break (double newline) */
    for (int offset = search_start;
         offset < target_offset + search_window && offset < max_offset;
         offset++)
    {
        if (offset > 0 && text[offset-1] == '\n' && text[offset] == '\n')
            return offset;
    }
 
    /* Look for sentence break (period, question mark, exclamation) */
    for (int offset = search_start;
         offset < target_offset + search_window && offset < max_offset;
         offset++)
    {
        if (offset > 0 &&
            (text[offset-1] == '.' || text[offset-1] == '?' || text[offset-1] == '!') &&
            (text[offset] == ' ' || text[offset] == '\n'))
        {
            return offset;
        }
    }
 
    /* Look for word break (space) */
    for (int offset = search_start;
         offset < target_offset + search_window && offset < max_offset;
         offset++)
    {
        if (text[offset] == ' ' || text[offset] == '\n')
            return offset;
    }
 
    /* Fall back to target offset */
    return best_offset < max_offset ? best_offset : max_offset;
}