Skip to content

Commit

Permalink
adding prompt context
Browse files Browse the repository at this point in the history
  • Loading branch information
Imsharad committed Feb 12, 2025
1 parent 26d7da0 commit b2218ff
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 44 deletions.
5 changes: 5 additions & 0 deletions packages/adapter-postgres/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,8 @@ CREATE INDEX IF NOT EXISTS idx_knowledge_shared ON knowledge("isShared");
CREATE INDEX IF NOT EXISTS idx_knowledge_embedding ON knowledge USING ivfflat (embedding vector_cosine_ops);

COMMIT;

-- Remove DEFAULT clauses since code provides empty strings
ALTER TABLE traces
ALTER COLUMN raw_context DROP DEFAULT,
ALTER COLUMN raw_response DROP DEFAULT;
33 changes: 32 additions & 1 deletion packages/adapter-postgres/tracing-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ CREATE TABLE IF NOT EXISTS traces (
session_id VARCHAR(256),
environment VARCHAR(64),
room_id VARCHAR(256),
raw_context TEXT NOT NULL DEFAULT '',
raw_response TEXT NOT NULL DEFAULT '',
PRIMARY KEY (trace_id, span_id)
);

Expand Down Expand Up @@ -50,4 +52,33 @@ CREATE TABLE IF NOT EXISTS events (
CREATE INDEX idx_events_agent ON events (agent_id);
CREATE INDEX idx_events_type ON events (event_type);
CREATE INDEX idx_events_time ON events (event_time);
CREATE INDEX idx_events_room ON events (room_id);
CREATE INDEX idx_events_room ON events (room_id);

-- Remove strict constraints temporarily
ALTER TABLE traces
DROP CONSTRAINT IF EXISTS raw_context_not_empty,
DROP CONSTRAINT IF EXISTS raw_response_not_empty;

-- Keep NOT NULL but allow empty strings
ALTER TABLE traces
ALTER COLUMN raw_context DROP DEFAULT,
ALTER COLUMN raw_response DROP DEFAULT;

-- Add smarter constraints that allow placeholders
ALTER TABLE traces
DROP CONSTRAINT valid_raw_context,
DROP CONSTRAINT valid_raw_response;

ALTER TABLE traces
ADD CONSTRAINT valid_raw_context
CHECK (
(span_name = 'llm_context_pre' AND raw_context <> '')
OR (span_name <> 'llm_context_pre')
);

ALTER TABLE traces
ADD CONSTRAINT valid_raw_response
CHECK (
(span_name = 'llm_response_post' AND raw_response <> '')
OR (span_name <> 'llm_response_post')
);
46 changes: 32 additions & 14 deletions packages/core/src/dbSpanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ async function insertTrace(spanData: any): Promise<void> {
agent_id,
session_id,
environment,
room_id
room_id,
raw_context,
raw_response
)
VALUES (
$1, $2, $3, $4, $5,
$6, $7, $8, $9, $10,
$11, $12, $13, $14, $15,
$16, $17, $18, $19
$16, $17, $18, $19, $20, $21
)
ON CONFLICT (trace_id, span_id) DO NOTHING;
`;
Expand All @@ -64,6 +66,8 @@ async function insertTrace(spanData: any): Promise<void> {
spanData.session_id || null,
spanData.environment || null,
spanData.room_id || null,
spanData.raw_context || '',
spanData.raw_response || '',
];

try {
Expand All @@ -84,6 +88,10 @@ export class DBSpanProcessor implements SpanProcessor {
}

async onEnd(span: ReadableSpan): Promise<void> {
if (!['llm_context_pre', 'llm_response_post'].includes(span.name)) {
return; // Skip non-LLM spans
}

const spanContext = span.spanContext();
console.log('Span Context:', spanContext);
console.log('Span Whole:', span);
Expand All @@ -96,10 +104,11 @@ export class DBSpanProcessor implements SpanProcessor {
const attributes = span.attributes || {};
const resource = span.resource?.attributes || {};

const safeTrim = (value: unknown): string | null => {
if (typeof value !== 'string') return null;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : null;
// Add truncation here
const MAX_CONTEXT_LENGTH = 4000;
const safeTrim = (value: unknown) => {
if (typeof value !== 'string') return '';
return value.trim().substring(0, MAX_CONTEXT_LENGTH);
};

const spanData = {
Expand All @@ -118,21 +127,30 @@ export class DBSpanProcessor implements SpanProcessor {
links: span.links || [],
resource: resource,
agent_id: safeTrim(attributes.agentId),
session_id: safeTrim(attributes["session.id"]),
environment: safeTrim(attributes["environment"]) ||
safeTrim(resource["deployment.environment"]) ||
'unknown',
room_id: safeTrim(attributes["room.id"]),
session_id: safeTrim(attributes['session.id']),
environment:
safeTrim(attributes['environment']) ||
safeTrim(resource['deployment.environment']) ||
'unknown',
room_id: safeTrim(attributes['room.id']),
raw_context: safeTrim(attributes.raw_context) || '',
raw_response: safeTrim(attributes.raw_response) || '',
};

// Add validation
if (!spanData.agent_id && !spanData.session_id && !spanData.room_id) {
console.log('⚠️ Skipping span with no context IDs:', span.name);
// Modify the validation check to allow spans with raw_context/response
if (
!spanData.agent_id &&
!spanData.session_id &&
!spanData.room_id
) {
console.log('⚠️ Skipping span...');
return;
}

console.log('🟡 Span ended, inserting:', span.name, spanData);

console.log('Span attributes:', JSON.stringify(attributes, null, 2));

try {
await insertTrace(spanData);
} catch (error) {
Expand Down
Loading

0 comments on commit b2218ff

Please sign in to comment.