Introduce Expert Agent. Prompt agents from UI.
This commit is contained in:
1 parent
a7cc529e38
commit
7853b71587
6 files changed
+194
-28
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
from .character import CharacterAgent
|
||||||
|
from .expert import ExpertAgent
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import re
|
||||||
|
import json
|
||||||
|
import inspect
|
||||||
|
from string import Template
|
||||||
|
|
||||||
|
from inference import LlamaMessage, replicate_llama_completion, Models
|
||||||
|
from agent import Agent, AgentField
|
||||||
|
|
||||||
|
CLASS_DESCRIPTION = """
|
||||||
|
Expert AI in any field, powered by Llama 3 70b.
|
||||||
|
Will get guidelines, documents, and other advanced features in the future.
|
||||||
|
""".strip()
|
||||||
|
|
||||||
|
class ExpertAgent(Agent):
|
||||||
|
identifier = "expert"
|
||||||
|
description = CLASS_DESCRIPTION
|
||||||
|
schema_fields = [
|
||||||
|
AgentField('system_prompt', 'string', default="You are an expert AI assistant."),
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self, app, data):
|
||||||
|
Agent.__init__(self, app, data)
|
||||||
|
|
||||||
|
async def prompt(self, instruction, input=None, **kwargs):
|
||||||
|
# if instruction and input are passed, instruction extends the system prompt
|
||||||
|
# otherwise, use the one passed as the user prompt, with the regular system prompt
|
||||||
|
|
||||||
|
# TODO: if 'history' in kwargs: interpret it as user-agent turns assuming list[str]
|
||||||
|
|
||||||
|
system = self.config['system_prompt']
|
||||||
|
if instruction != None and input != None:
|
||||||
|
system += "\n" + instruction
|
||||||
|
|
||||||
|
prompt = input if input != None else instruction
|
||||||
|
messages = [
|
||||||
|
LlamaMessage("system", system),
|
||||||
|
LlamaMessage("user", prompt)
|
||||||
|
]
|
||||||
|
|
||||||
|
response = await replicate_llama_completion(self.app, messages, model=Models.llama_70b)
|
||||||
|
return response
|
||||||
|
|
||||||
|
async def received_message(self, message):
|
||||||
|
raise Exception("TODO: expert chat support")
|
||||||
|
|
||||||
|
messages = []
|
||||||
|
response = await replicate_llama_completion(self.app, messages, model=Models.llama_70b)
|
||||||
|
self.app.send_chat_message(
|
||||||
|
chat_id=message.chat_id,
|
||||||
|
sender=self.id,
|
||||||
|
content=response,
|
||||||
|
thread_id=message.thread_id,
|
||||||
|
)
|
||||||
+9
-8
@@ -12,10 +12,10 @@ import replicate
|
|||||||
|
|
||||||
import database as db
|
import database as db
|
||||||
import agent as agent_mod
|
import agent as agent_mod
|
||||||
|
import agents as agents_mod
|
||||||
from server import start_server
|
from server import start_server
|
||||||
from chat import ChatView, Message, MESSAGE_PAGE_SIZE
|
from chat import ChatView, Message, MESSAGE_PAGE_SIZE
|
||||||
|
|
||||||
from agents.character import CharacterAgent
|
|
||||||
from app_mixins import ChatMixin
|
from app_mixins import ChatMixin
|
||||||
|
|
||||||
class App(ChatMixin):
|
class App(ChatMixin):
|
||||||
@@ -37,12 +37,13 @@ class App(ChatMixin):
|
|||||||
# `Agent` is the kind 'basic', will be class `BasicAgent` in the future
|
# `Agent` is the kind 'basic', will be class `BasicAgent` in the future
|
||||||
self.agent_classes = [
|
self.agent_classes = [
|
||||||
agent_mod.BasicAgent,
|
agent_mod.BasicAgent,
|
||||||
CharacterAgent,
|
agents_mod.CharacterAgent,
|
||||||
|
agents_mod.ExpertAgent,
|
||||||
|
|
||||||
agent_mod.LlamaAgent,
|
#agent_mod.LlamaAgent,
|
||||||
agent_mod.CoderV1Agent,
|
#agent_mod.CoderV1Agent,
|
||||||
agent_mod.ToolUsingAgent,
|
#agent_mod.ToolUsingAgent,
|
||||||
agent_mod.DebugAgent,
|
#agent_mod.DebugAgent,
|
||||||
]
|
]
|
||||||
|
|
||||||
def sql_session(self):
|
def sql_session(self):
|
||||||
@@ -231,9 +232,9 @@ class App(ChatMixin):
|
|||||||
return fields
|
return fields
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
async def send_agent_prompt(self, agent_id, message):
|
async def send_agent_prompt(self, agent_id, instruction, input=None, options={}):
|
||||||
control = self.get_agent_control(agent_id)
|
control = self.get_agent_control(agent_id)
|
||||||
response = await control.prompt(message)
|
response = await control.prompt(instruction, input, **options)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
async def call_agent_action(self, agent_id, action, args):
|
async def call_agent_action(self, agent_id, action, args):
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import threading
|
import threading
|
||||||
from stream import Stream
|
from stream import Stream
|
||||||
|
|
||||||
LLAMA3_MODEL = "meta/meta-llama-3-8b-instruct"
|
|
||||||
VALID_ROLES = ['user', 'agent', 'system']
|
VALID_ROLES = ['user', 'agent', 'system']
|
||||||
|
|
||||||
class LlamaMessage:
|
class LlamaMessage:
|
||||||
|
|||||||
+10
-1
@@ -224,7 +224,16 @@ def start_server(app):
|
|||||||
await send({'message': "chat thread created", 'threadId': thread_id, 'nonce': nonce})
|
await send({'message': "chat thread created", 'threadId': thread_id, 'nonce': nonce})
|
||||||
|
|
||||||
elif action == "send-agent-prompt":
|
elif action == "send-agent-prompt":
|
||||||
response = await app.send_agent_prompt(body['agentId'], body['content'])
|
if 'instruction' not in body and 'input' not in body:
|
||||||
|
await send({'error': "instruction or input required"})
|
||||||
|
continue
|
||||||
|
|
||||||
|
response = await app.send_agent_prompt(
|
||||||
|
body['agentId'],
|
||||||
|
body.get('instruction'),
|
||||||
|
body.get('input'),
|
||||||
|
body.get('options', {})
|
||||||
|
)
|
||||||
await send({'message': "propmt sent", 'response': response, 'nonce': nonce})
|
await send({'message': "propmt sent", 'response': response, 'nonce': nonce})
|
||||||
|
|
||||||
elif action == "call-agent-action":
|
elif action == "call-agent-action":
|
||||||
|
|||||||
+120
-18
@@ -1,6 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import {
|
import {
|
||||||
ref, watch,
|
ref, watch, reactive,
|
||||||
onMounted, onUnmounted,
|
onMounted, onUnmounted,
|
||||||
} from "vue";
|
} from "vue";
|
||||||
|
|
||||||
@@ -60,6 +60,67 @@
|
|||||||
loadAgentData(props.id);
|
loadAgentData(props.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// === Prompt === //
|
||||||
|
const promptInstructionEnabled = ref(false);
|
||||||
|
const promptInstruction = ref("");
|
||||||
|
const promptInput = ref("");
|
||||||
|
|
||||||
|
const promptInputElem = ref(null);
|
||||||
|
const promptHistory = ref([
|
||||||
|
//{instruction: 'Do something', input: 'With this data', output: "Doing thing with the data"},
|
||||||
|
//{input: 'Do this thing specifically', output: "Ok, I'll do the thing"},
|
||||||
|
//{input: 'Now, do something else', loading: true},
|
||||||
|
]);
|
||||||
|
|
||||||
|
async function sendPrompt () {
|
||||||
|
let instruction = promptInstruction.value;
|
||||||
|
let input = promptInput.value;
|
||||||
|
|
||||||
|
promptInstruction.value = "";
|
||||||
|
promptInput.value = "";
|
||||||
|
|
||||||
|
let payload = {agentId: agent.value.id};
|
||||||
|
let historyEntry = reactive({loading: true});
|
||||||
|
|
||||||
|
if (promptInstructionEnabled.value) {
|
||||||
|
payload.instruction = instruction;
|
||||||
|
payload.input = input;
|
||||||
|
|
||||||
|
historyEntry.instruction = instruction;
|
||||||
|
historyEntry.input = input;
|
||||||
|
} else {
|
||||||
|
// uhm
|
||||||
|
payload.instruction = input;
|
||||||
|
historyEntry.input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
promptHistory.value.push(historyEntry);
|
||||||
|
let res = await backend.sendAction("send-agent-prompt", payload);
|
||||||
|
|
||||||
|
if (res.error) {
|
||||||
|
historyEntry.error = res.error;
|
||||||
|
} else {
|
||||||
|
historyEntry.output = res.response;
|
||||||
|
}
|
||||||
|
historyEntry.loading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function promptKeyDown (event, stage) {
|
||||||
|
if (event.key == "Enter" && event.ctrlKey) {
|
||||||
|
event.preventDefault();
|
||||||
|
switch (stage) {
|
||||||
|
case 1:
|
||||||
|
promptInputElem.value.focus();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
sendPrompt();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Configuration === //
|
||||||
|
|
||||||
function updateIdentifier() {
|
function updateIdentifier() {
|
||||||
const value = prompt(`Enter a new identifier for ${agent.value.name}`);
|
const value = prompt(`Enter a new identifier for ${agent.value.name}`);
|
||||||
if (value) {
|
if (value) {
|
||||||
@@ -129,22 +190,55 @@
|
|||||||
<main>
|
<main>
|
||||||
<h1>{{ agent.name || `Agent-${id}` }}</h1>
|
<h1>{{ agent.name || `Agent-${id}` }}</h1>
|
||||||
<section class="top-section">
|
<section class="top-section">
|
||||||
<div>
|
<p style="white-space: pre-wrap; padding-bottom: 1em; border-bottom: 1px solid var(--color-border)">{{ kindInfo.description }}</p>
|
||||||
<span class="label">Agent ID</span>
|
|
||||||
<span class="value">{{id}}</span>
|
<div class="basic-config" style="padding-top: 1em;">
|
||||||
</div>
|
<div>
|
||||||
<div>
|
<span class="label">Agent ID</span>
|
||||||
<span class="label">Identifier</span>
|
<span class="value">{{id}}</span>
|
||||||
<span class="value">{{agent.agentname}}</span>
|
</div>
|
||||||
<button @click="updateIdentifier">Edit</button>
|
<div>
|
||||||
</div>
|
<span class="label">Identifier</span>
|
||||||
<div>
|
<span class="value">{{agent.agentname}}</span>
|
||||||
<button @click="updateName">Change Name</button>
|
<button @click="updateIdentifier">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<button @click="updateName">Change Name</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<section>
|
<section>
|
||||||
<p style="padding-bottom: 1em; border-bottom: 1px solid var(--color-border)">{{ kindInfo.description }}</p>
|
<h3>Prompt</h3>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<span class="label">Enable Instruction/Input</span>
|
||||||
|
<input type="checkbox" v-model="promptInstructionEnabled" />
|
||||||
|
</div>
|
||||||
|
<textarea v-if="promptInstructionEnabled" v-model="promptInstruction" class="config-input" @keydown="promptKeyDown($event, 1)" />
|
||||||
|
<textarea v-model="promptInput" class="config-input" ref="promptInputElem" @keydown="promptKeyDown($event, 2)" />
|
||||||
|
<div>
|
||||||
|
<button @click="sendPrompt()">Send Prompt</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-for="entry in promptHistory" class="prompt-history-entry">
|
||||||
|
<div v-if="entry.instruction">
|
||||||
|
<div class="label">Instruction</div>
|
||||||
|
<pre>{{entry.instruction}}</pre>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div class="label" v-if="entry.instruction">Input</div>
|
||||||
|
<div class="label" v-else>Prompt</div>
|
||||||
|
<pre>{{entry.input}}</pre>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div class="label">Output</div>
|
||||||
|
<div v-if="entry.loading">Loading...</div>
|
||||||
|
<pre v-if="entry.output">{{entry.output}}</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</section>
|
||||||
|
<section>
|
||||||
<div v-for="(configField, index) in configFields" :key="index">
|
<div v-for="(configField, index) in configFields" :key="index">
|
||||||
<span class="label">{{ configField.name }}</span>
|
<span class="label">{{ configField.name }}</span>
|
||||||
<input type="checkbox" v-model="configField.enabled" />
|
<input type="checkbox" v-model="configField.enabled" />
|
||||||
@@ -165,13 +259,21 @@
|
|||||||
border-radius: var(--border-radius);
|
border-radius: var(--border-radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
.top-section {
|
.basic-config > div {
|
||||||
display: flex;
|
margin-right: 2em;
|
||||||
flex-direction: row;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.top-section > div {
|
.prompt-history-entry {
|
||||||
margin-right: 2em;
|
border-top: 1px dashed var(--color-border);
|
||||||
|
padding: 1em 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.prompt-history-entry pre {
|
||||||
|
border: 1px solid var(--color-border);
|
||||||
|
border-radius: var(--border-radius);
|
||||||
|
padding: 0.5em;
|
||||||
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
.label {
|
.label {
|
||||||
|
|||||||
Reference in new issue
Block a user