-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add feedback and feedback_task with cli (#102)
* add log10 feedback or log10 feedback-task create and cli * update examples/feedback/simple_feedback.py * update README --------- Co-authored-by: Kim Tran <[email protected]>
- Loading branch information
1 parent
be05595
commit 67e4c48
Showing
6 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import uuid | ||
from typing import Literal | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from log10.feedback.feedback import Feedback | ||
from log10.feedback.feedback_task import FeedbackTask | ||
from log10.load import OpenAI | ||
|
||
|
||
# | ||
# use log10 to log an openai completion | ||
# | ||
|
||
# create a unique id | ||
unique_id = str(uuid.uuid4()) | ||
print(f"Use tag: {unique_id}") | ||
client = OpenAI(tags=[unique_id]) | ||
completion = client.chat.completions.create( | ||
model="gpt-3.5-turbo", | ||
messages=[ | ||
{ | ||
"role": "system", | ||
"content": "You are the most knowledgable Star Wars guru on the planet", | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Write the time period of all the Star Wars movies and spinoffs?", | ||
}, | ||
], | ||
) | ||
print(completion.choices[0].message) | ||
|
||
# | ||
# add feedback to the completion | ||
# | ||
|
||
|
||
# define a feedback task | ||
class EmojiFeedback(BaseModel): | ||
feedback: Literal["😀", "🙁"] = Field(..., description="User feedback with emojis") | ||
|
||
|
||
# create a feedback | ||
fb = EmojiFeedback(feedback="😀") | ||
|
||
task = FeedbackTask().create(name="emoji_task_test", task_schema=fb.model_json_schema()) | ||
task_dump = task.json() | ||
|
||
print(fb.model_dump_json()) | ||
Feedback().create(task_id=task_dump["id"], values=fb.model_dump(), completion_tags_selector=[unique_id]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import click | ||
|
||
from log10.feedback.feedback import create_feedback | ||
from log10.feedback.feedback_task import create_feedback_task | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
@click.group() | ||
def feedback(): | ||
pass | ||
|
||
|
||
@click.group() | ||
def feedback_task(): | ||
pass | ||
|
||
|
||
cli.add_command(feedback) | ||
feedback.add_command(create_feedback, "create") | ||
cli.add_command(feedback_task) | ||
feedback_task.add_command(create_feedback_task, "create") | ||
|
||
if __name__ == "__main__": | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
import logging | ||
|
||
import click | ||
import httpx | ||
from dotenv import load_dotenv | ||
|
||
from log10.llm import Log10Config | ||
|
||
|
||
load_dotenv() | ||
|
||
logging.basicConfig( | ||
format="[%(asctime)s - %(name)s - %(levelname)s] %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
) | ||
logger: logging.Logger = logging.getLogger("LOG10") | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
class Feedback: | ||
feedback_create_url = "api/v1/feedback" | ||
|
||
def __init__(self, log10_config: Log10Config = None): | ||
self._log10_config = log10_config or Log10Config() | ||
self._http_client = httpx.Client() | ||
|
||
def _post_request(self, url: str, json_payload: dict) -> httpx.Response: | ||
headers = { | ||
"x-log10-token": self._log10_config.token, | ||
"x-log10-organization-id": self._log10_config.org_id, | ||
"Content-Type": "application/json", | ||
} | ||
json_payload["organization_id"] = self._log10_config.org_id | ||
try: | ||
res = self._http_client.post(self._log10_config.url + url, headers=headers, json=json_payload) | ||
res.raise_for_status() | ||
return res | ||
except Exception as e: | ||
logger.error(e) | ||
logger.error(e.response.json()["error"]) | ||
raise | ||
|
||
def create( | ||
self, task_id: str, values: dict, completion_tags_selector: list[str], comment: str = None | ||
) -> httpx.Response: | ||
json_payload = { | ||
"task_id": task_id, | ||
"json_values": values, | ||
"completion_tags_selector": completion_tags_selector, | ||
} | ||
res = self._post_request(self.feedback_create_url, json_payload) | ||
return res | ||
|
||
|
||
@click.command() | ||
@click.option("--task_id", prompt="Enter task id", help="Task ID") | ||
@click.option("--values", prompt="Enter task values", help="Feedback in JSON format") | ||
@click.option("--completion_tags_selector", prompt="Enter completion tags selector", help="Completion tags selector") | ||
def create_feedback(task_id, values, completion_tags_selector): | ||
click.echo("Creating feedback") | ||
tags = completion_tags_selector.split(",") | ||
values = json.loads(values) | ||
feedback = Feedback().create(task_id=task_id, values=values, completion_tags_selector=tags) | ||
click.echo(feedback.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import json | ||
import logging | ||
|
||
import click | ||
import httpx | ||
from dotenv import load_dotenv | ||
|
||
from log10.llm import Log10Config | ||
|
||
|
||
load_dotenv() | ||
|
||
logging.basicConfig( | ||
format="[%(asctime)s - %(name)s - %(levelname)s] %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
) | ||
logger: logging.Logger = logging.getLogger("LOG10") | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
class FeedbackTask: | ||
feedback_task_create_url = "api/v1/feedback_task" | ||
|
||
def __init__(self, log10_config: Log10Config = None): | ||
self._log10_config = log10_config or Log10Config() | ||
self._http_client = httpx.Client() | ||
|
||
def _post_request(self, url: str, json_payload: dict) -> httpx.Response: | ||
headers = { | ||
"x-log10-token": self._log10_config.token, | ||
"Content-Type": "application/json", | ||
"x-log10-organization-id": self._log10_config.org_id, | ||
} | ||
json_payload["organization_id"] = self._log10_config.org_id | ||
try: | ||
res = self._http_client.post(self._log10_config.url + url, headers=headers, json=json_payload) | ||
res.raise_for_status() | ||
return res | ||
except Exception as e: | ||
logger.error(e) | ||
logger.error(e.response.json()["error"]) | ||
raise | ||
|
||
def create(self, task_schema: dict, name: str = None, instruction: str = None) -> httpx.Response: | ||
json_payload = {"json_schema": task_schema} | ||
if name: | ||
json_payload["name"] = name | ||
if instruction: | ||
json_payload["instruction"] = instruction | ||
|
||
res = self._post_request(self.feedback_task_create_url, json_payload) | ||
return res | ||
|
||
|
||
# create a cli interface for FeebackTask.create function | ||
@click.command() | ||
@click.option("--name", prompt="Enter feedback task name", help="Name of the task") | ||
@click.option("--task_schema", prompt="Enter feedback task schema", help="Task schema") | ||
def create_feedback_task(name, task_schema): | ||
click.echo("Creating feedback task") | ||
task_schema = json.loads(task_schema) | ||
task = FeedbackTask().create(name=name, task_schema=task_schema) | ||
click.echo(f"Use this task_id to add feedback: {task.json()['id']}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters