Skip to content

Commit

Permalink
feat: replacing placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
gumberss committed Aug 2, 2024
1 parent 674b5ac commit cb192f9
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 33 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion services/llm-client/adapters/db/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from utils import json

def request_to_model(wire):
return InteractionRequest(wire.id, json.deserialize(wire.interaction, Interaction) , wire.response, wire.request_date, wire.status, wire.timeout)
return InteractionRequest(wire.id, json.deserialize(wire.interaction, Interaction), wire.response, wire.request_date, wire.status, wire.timeout, wire.details)
Binary file not shown.
23 changes: 13 additions & 10 deletions services/llm-client/diplomat/db/interactions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from components.scylla_connection import ScyllaConnection
from datetime import datetime, timezone
from adapters.db import interactions as interactions_db_adapter
from models.interactions import InteractionRequest
from utils import json

def get_interaction(interaction_id, scylla: ScyllaConnection):
Expand All @@ -10,24 +11,26 @@ def get_interaction(interaction_id, scylla: ScyllaConnection):
return interactions_db_adapter.request_to_model(rows[0]) if rows else None

def insert(interaction, scylla: ScyllaConnection):
print(json.serialize(interaction))
statement = """INSERT INTO execution.interaction_requests (id, interaction, response, request_date, status, timeout)
VALUES (?, ?, ?, ?, ?, ?)"""
statement = """INSERT INTO execution.interaction_requests (id, interaction, response, request_date, status, timeout, details)
VALUES (?, ?, ?, ?, ?, ?, ?)"""
interaction_id = interaction.id
interaction_value = json.serialize(interaction)
response = None
request_date = int(datetime.now(timezone.utc).timestamp() * 1000)
status = 'pending'
timeout = 30000
details = None
prepared_statement = scylla.session.prepare(statement)
scylla.session.execute(prepared_statement, (interaction_id, interaction_value, response, request_date, status, timeout))
scylla.session.execute(prepared_statement, (interaction_id, interaction_value, response, request_date, status, timeout, details))
return InteractionRequest(interaction_id,interaction, response, request_date, status, timeout, details)


def update_interaction(interaction_result, scylla: ScyllaConnection):
def update_interaction(interaction_request: InteractionRequest, scylla: ScyllaConnection):
statement = """UPDATE execution.interaction_requests
SET response = %s, status = %s
SET response = %s, status = %s, details = %s
WHERE id = %s"""
interaction_id = interaction_result.id
response = interaction_result.response
status = interaction_result.status
scylla.session.execute(statement, (response, status, interaction_id))
interaction_id = interaction_request.id
response = interaction_request.response
status = interaction_request.status
details = interaction_request.details
scylla.session.execute(statement, (response, status, details, interaction_id))
Binary file modified services/llm-client/flows/__pycache__/interactions.cpython-39.pyc
Binary file not shown.
29 changes: 22 additions & 7 deletions services/llm-client/flows/interactions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

from components.scylla_connection import ScyllaConnection
from datetime import datetime, timezone
from models.interactions import Interaction, InteractionResult
from models.interactions import Interaction
from diplomat.db import interactions as interactions_db
from diplomat.db import prompts as prompts_db
from logic import interactions as interactions_logic
from logic import prompts as prompts_logic

def new_interaction(interaction: Interaction, scylla: ScyllaConnection):
existent_interaction = interactions_db.get_interaction(interaction.id, scylla)
Expand All @@ -13,22 +14,36 @@ def new_interaction(interaction: Interaction, scylla: ScyllaConnection):
return existent_interaction
else:
if(interactions_logic.is_timed_out(existent_interaction, int(datetime.now(timezone.utc).timestamp() * 1000))):
existent_interaction.failed()
interactions_db.update_interaction(InteractionResult(existent_interaction.id, 'failed', None), scylla)
existent_interaction.failed("Timed out")
interactions_db.update_interaction(existent_interaction, scylla)
return existent_interaction
else:
return existent_interaction

interaction_request = interactions_db.insert(interaction, scylla)

prompt = prompts_db.get_prompt(interaction.prompt_name, scylla)
if(prompt is None):
return None
interaction_request.failed("Prompt not found")
interactions_db.update_interaction(interaction_request, scylla)
return interaction_request

# fill prompt with variables
interactions_db.insert(interaction, scylla)
prompt_variables = prompts_logic.find_variables(prompt)
missing_variables = prompts_logic.find_missing_variables(prompt_variables, interaction.variables)

if(missing_variables):
missing_variables = ", ".join(missing_variables)
interaction_request.failed(f"Variables missing: {missing_variables}")
interactions_db.update_interaction(interaction_request, scylla)
return interaction_request

rendered_prompt = prompts_logic.replace_variables(prompt, interaction.variables)


# request Open IA
# update the database
# return
return interaction
return rendered_prompt



Binary file modified services/llm-client/logic/__pycache__/prompts.cpython-39.pyc
Binary file not shown.
14 changes: 13 additions & 1 deletion services/llm-client/logic/prompts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from models.prompts import Prompt
from typing import List, Dict, Any
import re

def find_variables(prompt : Prompt):
pattern = r'\{\{(\w+)\}\}'
return re.findall(pattern, prompt.prompt)
return re.findall(pattern, prompt.prompt)

def find_missing_variables(prompt_variables: List[str], received_variables: Dict[str, Any]) -> List[str]:
missing_vars = []
for var in prompt_variables:
if var not in received_variables or received_variables[var] is None:
missing_vars.append(var)
return missing_vars

def replace_variables(prompt: Prompt, variables: dict[str, Any]) -> str:
pattern = re.compile(r"\{\{(.*?)\}\}")
return pattern.sub(lambda match: str(variables.get(match.group(1), match.group(0))), prompt.prompt)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def run_migration(session):
response text,
request_date bigint,
status text,
timeout int
timeout int,
details text
);
""")
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
def run_migration(session):
session.execute("""
INSERT INTO configuration.prompts (prompt_name, prompt)
VALUES('tick_shopping_items', 'Are you able to tick items on a cart if I send you a picture?')
VALUES('tick_shopping_items', 'Are you able to tick items on a cart if I send you a {{item_name}}?')
""")

Binary file not shown.
Binary file not shown.
Binary file modified services/llm-client/models/__pycache__/interactions.cpython-39.pyc
Binary file not shown.
14 changes: 5 additions & 9 deletions services/llm-client/models/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ def __init__(self, id: UUID, prompt_name:str, variables:dict, images:dict, messa
self.message = message

class InteractionRequest:
def __init__(self, id, interaction, response, request_date, status, timeout):
def __init__(self, id, interaction, response, request_date, status, timeout, details):
self.id = id
self.interaction = interaction
self.response = response
self.request_date = request_date
self.status = status
self.timeout = timeout
self.details = details

def failed(self):
def failed(self, details: str):
self.status = 'failed'
self.details = details
return self


class InteractionResult:
def __init__(self, id, status, response):
self.id = id
self.status = status
self.response = response

2 changes: 0 additions & 2 deletions services/llm-client/pytest.ini

This file was deleted.

Binary file not shown.
63 changes: 62 additions & 1 deletion services/llm-client/tests/logic/prompts_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from logic import prompts
from typing import List, Dict, Any
from models.prompts import Prompt

def test_find_variables_basic():
Expand All @@ -20,4 +21,64 @@ def test_edge_case_empty_string():

def test_edge_case_empty_braces():
prompt = Prompt("", "{{}} should not be considered a variable.")
assert prompts.find_variables(prompt) == []
assert prompts.find_variables(prompt) == []

def find_missing_variables(variables: List[str], values: Dict[str, Any]) -> List[str]:
missing_vars = []
for var in variables:
if var not in values or values[var] is None:
missing_vars.append(var)
return missing_vars

def test_find_missing_variables_all_present():
variables: List[str] = ["var1", "var2", "var3"]
values: Dict[str, Any] = {
"var1": "value1",
"var2": "value2",
"var3": "value3"
}
assert find_missing_variables(variables, values) == []

def test_find_missing_variables_some_missing():
variables: List[str] = ["var1", "var2", "var3"]
values: Dict[str, Any] = {
"var1": "value1",
"var2": "value2"
}
assert find_missing_variables(variables, values) == ["var3"]

def test_find_missing_variables_some_none():
variables: List[str] = ["var1", "var2", "var3"]
values: Dict[str, Any] = {
"var1": "value1",
"var2": None,
"var3": "value3"
}
assert find_missing_variables(variables, values) == ["var2"]

def test_find_missing_variables_empty_variables():
variables: List[str] = []
values: Dict[str, Any] = {
"var1": "value1",
"var2": "value2",
"var3": "value3"
}
assert find_missing_variables(variables, values) == []

def test_find_missing_variables_empty_values():
variables: List[str] = ["var1", "var2", "var3"]
values: Dict[str, Any] = {}
assert find_missing_variables(variables, values) == ["var1", "var2", "var3"]

def test_replace_variables():
template = "Hello, {{name}}. You have {{count}} new messages."
values = {"name": "Alice", "count": 5}
assert prompts.replace_variables(template, values) == "Hello, Alice. You have 5 new messages."

template = "Hello, {{name}}. You have {{count}} new messages and your ID is {{id}}."
values = {"name": "Bob", "count": 10, "id": 12345}
assert prompts.replace_variables(template, values) == "Hello, Bob. You have 10 new messages and your ID is 12345."

template = "Hello, {{name}}."
values = {}
assert prompts.replace_variables(template, values) == "Hello, {{name}}."

0 comments on commit cb192f9

Please sign in to comment.