Skip to content

Commit

Permalink
Merge pull request #307 from ohdearquant/merge_direct
Browse files Browse the repository at this point in the history
Merge direct, vote
  • Loading branch information
ohdearquant authored Mar 26, 2024
2 parents 89383b9 + ef88c96 commit 510c513
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 329 deletions.
2 changes: 2 additions & 0 deletions lionagi/core/direct/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .predict import predict
from .select import select
from .score import score
from .vote import vote

__all__ = [
"predict",
"select",
"score",
"vote"
]
79 changes: 77 additions & 2 deletions lionagi/core/direct/score.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import Field

import numpy as np
from lionagi.libs import func_call, convert
from ..prompt.prompt_template import ScoredTemplate
from ..branch import Branch
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(
"precision": num_digit if num_digit != 0 else None,
}

async def score(
async def _score(
sentence,
instruction=None,
score_range=(1, 10),
Expand All @@ -80,6 +80,11 @@ async def score(
tool_manager=None,
**kwargs,
):

if "temperature" not in kwargs:
kwargs["temperature"] = 0.1

instruction = instruction or ""

branch = Branch(
name=branch_name,
Expand Down Expand Up @@ -116,3 +121,73 @@ async def score(
)

return _template

# async def group_score(sentence, *args, num_instances=5, **kwargs):
# sentences = [sentence for _ in range(num_instances)]

# outs_ = await func_call.alcall(sentences, _score, *args, **kwargs)

# return np.mean([i.answer for i in outs_])


async def score(
sentence,
num_instances=1,
instruction=None,
score_range=(1, 10),
inclusive=True,
num_digit=0,
confidence_score=False,
reason=False,
retries=2,
delay=0.5,
backoff_factor=2,
default_value=None,
timeout=None,
branch_name=None,
system=None,
messages=None,
service=None,
sender=None,
llmconfig=None,
tools=None,
datalogger=None,
persist_path=None,
tool_manager=None,
return_template=True,
**kwargs,
):
async def _inner(i=0):
return await _score(
sentence=sentence,
instruction=instruction,
score_range=score_range,
inclusive=inclusive,
num_digit=num_digit,
confidence_score=confidence_score,
reason=reason,
retries=retries,
delay=delay,
backoff_factor=backoff_factor,
default_value=default_value,
timeout=timeout,
branch_name=branch_name,
system=system,
messages=messages,
service=service,
sender=sender,
llmconfig=llmconfig,
tools=tools,
datalogger=datalogger,
persist_path=persist_path,
tool_manager=tool_manager,
**kwargs
)

if num_instances == 1:
_out = await _inner()
return _out if return_template else _out.answer

elif num_instances > 1:
_outs = await func_call.alcall(range(num_instances), _inner)
return _outs if return_template else np.mean([i.answer for i in _outs])
6 changes: 2 additions & 4 deletions lionagi/core/direct/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(
self,
sentence=None,
choices=None,
num_choices=1,
instruction=None,
reason=False,
confidence_score=False,
Expand All @@ -27,7 +26,7 @@ def __init__(

self.sentence = sentence
self.choices = choices
self.task = f"select {num_choices} item(s), from provided choices {choices}."
self.task = f"select 1 item, from provided choices {choices}."
if instruction:
self.task += f"objetive {instruction}."

Expand All @@ -41,7 +40,6 @@ def __init__(
async def select(
sentence,
choices=None,
num_choices=1,
instruction=None,
confidence_score=False,
reason=False,
Expand Down Expand Up @@ -79,7 +77,6 @@ async def select(
_template = SelectTemplate(
sentence=sentence,
choices=choices,
num_choices=num_choices,
instruction=instruction,
confidence_score=confidence_score,
reason=reason,
Expand All @@ -97,6 +94,7 @@ async def select(
)

ans = _template.answer

if ans not in _template.choices:
_template.answer = StringMatch.choose_most_similar(ans, _template.choices)

Expand Down
30 changes: 30 additions & 0 deletions lionagi/core/direct/vote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from lionagi.libs import func_call
import numpy as np
from .predict import predict
from .score import score

# for example, directive=predict

async def vote(sentence, directive=predict, num_generations=5, num_output=1, num_scorer=5, score_range=(0,100), num_digit=2, scorer_instruction=None, **kwargs):

async def _inner(i):
out_ = await directive(sentence, **kwargs)
score_ = await score(
out_.answer,
context=sentence,
instruction=scorer_instruction,
score_range=score_range,
num_digit=num_digit,
num_instances=num_scorer,
return_template=False
)

out_.__setattr__('score', score_)
return out_

_outs = await func_call.alcall(list(range(num_generations)), _inner)

top_index = np.argsort([i.score for i in _outs])[-num_output:]
final_output = list(np.array(_outs)[top_index])

return final_output[0] if len(final_output) == 1 else final_output
2 changes: 1 addition & 1 deletion lionagi/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.310"
__version__ = "0.0.311"
2 changes: 1 addition & 1 deletion notebooks/lion_agent_with_tool.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 510c513

Please sign in to comment.