From 47aeb238a06b4327cdae22be81e6e02c0c620fe4 Mon Sep 17 00:00:00 2001 From: Abram Date: Fri, 15 Dec 2023 08:38:22 +0100 Subject: [PATCH] Feat - created llm example app for binaryparam --- examples/chat_json_format/app.py | 43 ++++++++++++++++++++++ examples/chat_json_format/requirements.txt | 2 + 2 files changed, 45 insertions(+) create mode 100644 examples/chat_json_format/app.py create mode 100644 examples/chat_json_format/requirements.txt diff --git a/examples/chat_json_format/app.py b/examples/chat_json_format/app.py new file mode 100644 index 0000000000..8f9d234480 --- /dev/null +++ b/examples/chat_json_format/app.py @@ -0,0 +1,43 @@ +import agenta as ag +from agenta.sdk.types import BinaryParam +from openai import OpenAI + +client = OpenAI() + +SYSTEM_PROMPT = "You have expertise in offering technical ideas to startups. Responses should be in json." +GPT_FORMAT_RESPONSE = ["gpt-3.5-turbo-1106", "gpt-4-1106-preview"] +CHAT_LLM_GPT = [ + "gpt-3.5-turbo-16k", + "gpt-3.5-turbo-0301", + "gpt-3.5-turbo-0613", + "gpt-3.5-turbo-16k-0613", + "gpt-4", +] + GPT_FORMAT_RESPONSE + +ag.init() +ag.config.default( + temperature=ag.FloatParam(0.2), + model=ag.MultipleChoiceParam("gpt-3.5-turbo", CHAT_LLM_GPT), + max_tokens=ag.IntParam(-1, -1, 4000), + prompt_system=ag.TextParam(SYSTEM_PROMPT), + force_json_response=BinaryParam(), +) + + +@ag.entrypoint +def chat(inputs: ag.MessagesInput = ag.MessagesInput()): + messages = [{"role": "system", "content": ag.config.prompt_system}] + inputs + max_tokens = ag.config.max_tokens if ag.config.max_tokens != -1 else None + response_format = ( + {"type": "json_object"} + if ag.config.force_json_response and ag.config.model in GPT_FORMAT_RESPONSE + else {"type": "text"} + ) + chat_completion = client.chat.completions.create( + model=ag.config.model, + messages=messages, + temperature=ag.config.temperature, + max_tokens=max_tokens, + response_format=response_format, + ) + return chat_completion.choices[0].message.content diff --git a/examples/chat_json_format/requirements.txt b/examples/chat_json_format/requirements.txt new file mode 100644 index 0000000000..310f162cec --- /dev/null +++ b/examples/chat_json_format/requirements.txt @@ -0,0 +1,2 @@ +agenta +openai \ No newline at end of file