Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/llm client #257

Merged
merged 5 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ pom.xml.asc
!/infra/docker-compose.yml
.idea/
.clj-kondo/
.lsp/
.lsp/
**/.venv

**/__pycache__/

*.pyc
*.pyo

*.pyd
*.py[co]
Binary file not shown.
Binary file not shown.
42 changes: 29 additions & 13 deletions services/llm-client/diplomat/http_client.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@

from openai import OpenAI
from models.prompts import RenderedPrompt
import os;

client = OpenAI(
api_key=""
#os.environ['OPENAI_API_KEY'], # this is also the default, it can be omitted
api_key=os.getenv('OPENAI_API_KEY').strip()
)

def complete():
def complete(rendered_prompt : RenderedPrompt):

images = [{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image}"
}
} for image in rendered_prompt.images]

completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Hello, how are you?",
},
],
)
print(completion.choices[0].message.content)
return completion.choices[0].message.content
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": rendered_prompt.prompt
},
*images
]
}
],
)
return completion.choices[0].message.content

Binary file modified services/llm-client/flows/__pycache__/interactions.cpython-39.pyc
Binary file not shown.
39 changes: 27 additions & 12 deletions services/llm-client/flows/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
from components.scylla_connection import ScyllaConnection
from datetime import datetime, timezone
from models.interactions import Interaction
from models.prompts import Prompt, RenderedPrompt
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
from diplomat import http_client

def render_prompt(interaction: Interaction, prompt: Prompt) -> RenderedPrompt:
prompt_variables = prompts_logic.find_variables(prompt)
missing_variables = prompts_logic.find_missing_variables(prompt_variables, interaction.variables)
if (missing_variables):
missing_variables_str = ", ".join(missing_variables)
raise ValueError(f"Variables missing: {missing_variables_str}")
return RenderedPrompt(prompt.prompt_name, prompts_logic.replace_variables(prompt, interaction.variables), interaction.images)


def new_interaction(interaction: Interaction, scylla: ScyllaConnection):
existent_interaction = interactions_db.get_interaction(interaction.id, scylla)
Expand All @@ -28,22 +39,26 @@ def new_interaction(interaction: Interaction, scylla: ScyllaConnection):
interactions_db.update_interaction(interaction_request, scylla)
return interaction_request

prompt_variables = prompts_logic.find_variables(prompt)
missing_variables = prompts_logic.find_missing_variables(prompt_variables, interaction.variables)
rendered_prompt = None

if(missing_variables):
missing_variables = ", ".join(missing_variables)
interaction_request.failed(f"Variables missing: {missing_variables}")
try:
rendered_prompt = render_prompt(interaction, prompt)
except ValueError as e:
error_message = str(e)
interaction_request.failed(error_message)
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 rendered_prompt
try:
request_response = http_client.complete(rendered_prompt)
interaction_request.sucess(request_response)
interactions_db.update_interaction(interaction_request, scylla)
return interaction_request
except Exception as e:
error_message = str(e)
interaction_request.failed(error_message)
interactions_db.update_interaction(interaction_request, scylla)
return interaction_request



Binary file modified services/llm-client/models/__pycache__/interactions.cpython-39.pyc
Binary file not shown.
Binary file modified services/llm-client/models/__pycache__/prompts.cpython-39.pyc
Binary file not shown.
8 changes: 7 additions & 1 deletion services/llm-client/models/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def __init__(self, id, interaction, response, request_date, status, timeout, det
self.details = details

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

def sucess(self, response: str):
self.status = 'success'
self.details = ""
self.response = response
return self

9 changes: 8 additions & 1 deletion services/llm-client/models/prompts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class Prompt:
def __init__(self, prompt_name:str, prompt:str):
self.prompt_name = prompt_name
self.prompt = prompt
self.prompt = prompt

class RenderedPrompt:
def __init__(self, prompt_name:str, prompt:str, images:dict[str, str]):
self.prompt_name = prompt_name
self.prompt = prompt
self.images = images

3 changes: 2 additions & 1 deletion services/llm-client/readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Requirements
- cassandra-driver
- pip install cassandra-driver
- pip install 'uvicorn[standard]'
- pip install fastapi
- pip install pytest
- pip install openai

docker run -d --name some-scylla -p 9042:9042 scylladb/scylla
docker exec -it some-scylla cqlsh
Expand Down
Loading