-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea11e59
commit d0e1da8
Showing
14 changed files
with
163 additions
and
8 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
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
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,91 @@ | ||
import inspect | ||
import json | ||
|
||
type_mapping = { | ||
int: "integer", | ||
str: "string", | ||
float: "number", | ||
bool: "boolean", | ||
list: "array", | ||
dict: "object" | ||
} | ||
|
||
def describe_single(func): | ||
""" | ||
Takes a function and returns its metadata as a JSON string in the specified format. | ||
""" | ||
signature = inspect.signature(func) | ||
params = signature.parameters | ||
|
||
# Collecting function metadata | ||
func_name = func.__name__ | ||
func_doc = inspect.getdoc(func) or "" | ||
|
||
properties = {} | ||
for name, param in params.items(): | ||
param_type = param.annotation | ||
if param_type in type_mapping: | ||
json_type = type_mapping[param_type] | ||
else: | ||
json_type = "string" # default to string if type is not mapped | ||
properties[name] = {"type": json_type} | ||
|
||
result = { | ||
"name": func_name, | ||
"description": func_doc, | ||
"parameters": { | ||
"type": "object", | ||
"properties": properties, | ||
"required": list(properties.keys()) | ||
} | ||
} | ||
|
||
return result | ||
|
||
def describe(functions): | ||
return [describe_single(func) for func in functions] | ||
|
||
def complete(completion, messages = [], tools = [], show = True): | ||
tools = {func.__name__: func for func in tools} | ||
content = result= "" | ||
tool_name = tool_text = "" | ||
tool_args = None | ||
|
||
if not 'stream' in str(type(completion)).lower(): | ||
content = completion.choices[0].message.content | ||
if chunk.choices[0].message.function_call != None: | ||
tool_name = chunk.choices[0].message.function_call.name | ||
tool_args = json.loads(chunk.choices[0].message.function_call.arguments) | ||
if show: | ||
print(content) | ||
else: | ||
for chunk in completion: | ||
if chunk.choices and len(chunk.choices) > 0 and chunk.choices[0].delta: | ||
if chunk.choices[0].delta.content: | ||
if show: | ||
print(chunk.choices[0].delta.content, end="") | ||
content += chunk.choices[0].delta.content | ||
if chunk.choices[0].delta.function_call != None: | ||
tool_text += chunk.choices[0].delta.function_call.arguments | ||
if chunk.choices[0].delta.function_call.name: | ||
tool_name = chunk.choices[0].delta.function_call.name | ||
try: | ||
tool_args = json.loads(tool_text) | ||
except Exception as e: | ||
pass | ||
if show: | ||
print() | ||
|
||
if len(content) > 0: | ||
messages.append({ "role": "assistant", "content": content}) | ||
|
||
if tool_args: | ||
if tool_name in tools: | ||
try: | ||
result = str(tools[tool_name](**tool_args)) | ||
except Exception as e: | ||
result = str(e) | ||
print(result) | ||
messages.append({ "role": "function", "name": tool_name, "content": result}) | ||
|
||
return content + result |
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 @@ | ||
"" |
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 @@ | ||
�N. |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "hal9" | ||
version = "2.6.0" | ||
version = "2.6.1" | ||
description = "" | ||
authors = ["Javier Luraschi <[email protected]>"] | ||
readme = "README.md" | ||
|
File renamed without changes.
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,5 @@ | ||
import hal9 as h9 | ||
|
||
def test_save_empty_error(): | ||
h9.save("messages", None) | ||
assert True |
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,27 @@ | ||
# Tools | ||
|
||
This section presents how to add tools to your LLM application. | ||
|
||
```python | ||
import os | ||
from openai import OpenAI | ||
import hal9 as h9 | ||
|
||
def multiply(a: int, b: int) -> int: | ||
"""Multiply two numbers.""" | ||
return a * b | ||
|
||
messages = h9.load("messages", []) | ||
prompt = h9.input(messages = messages) | ||
|
||
completion = OpenAI().chat.completions.create( | ||
model = "gpt-4", | ||
messages = messages, | ||
functions = h9.describe([multiply]), | ||
function_call = "auto", | ||
stream = True | ||
) | ||
|
||
h9.complete(completion, messages = messages, functions = [multiply]) | ||
h9.save("messages", messages, hidden = True) | ||
``` |
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,22 @@ | ||
# Complete | ||
|
||
Convenience functions to handle LLM completions | ||
|
||
## Complete | ||
`complete(completion, messages, tools, show)` <br/><br/> | ||
Finishes completing the completions by printing them, appending messages, or handling tools. | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| completion | <code>String</code> | The completions form the LLM. | | ||
| messages | <code>Array</code> | Messages to append replies to, defaults to `[]`. | | ||
| tools | <code>Array</code> | An array of functions to use as tools, defaults `[]`. | | ||
| show | <code>Bool</code> | Print the completions? Defaults to `True`. | | ||
|
||
## Describe | ||
`describe(funcs)` <br/><br/> | ||
Describes an array of functions with descriptions, parameters and types. Useful when completing chats. | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| functions | <code>Array</code> | An array of functions to describe. | |
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