Skip to content

Commit

Permalink
v0.3.3 - add system prompt and messages to function completion
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Jul 31, 2023
1 parent 5ca6a3c commit 68c0fc0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ The response object looks like this:
}
```

### `function_completion(text, functions=None, model_failure_retries=5, function_call=None, function_failure_retries=10, chunk_length=DEFAULT_CHUNK_LENGTH, model=None, api_key=None)`
### `function_completion(text, functions=None, system_message=None, messages=None, model_failure_retries=5, function_call=None, function_failure_retries=10, chunk_length=DEFAULT_CHUNK_LENGTH, model=None, api_key=None)`

Sends text and a list of functions to the model and returns optional text and a function call. The function call is validated against the functions array.

Optionally takes a system message and a list of messages to send to the model before the function call. If messages are provided, the "text" becomes the last user message in the list.

```python
function = {
'name': 'function1',
Expand Down
14 changes: 12 additions & 2 deletions easycompletion/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ def text_completion(

def function_completion(
text,
system_message=None,
messages=None,
functions=None,
model_failure_retries=5,
function_call=None,
Expand Down Expand Up @@ -510,8 +512,16 @@ def function_completion(
log("Error: Message too long", type="error", log=debug)
return {"error": "Message too long"}

all_messages = []

if system_message is not None:
messages.append({"role": "system", "content": system_message})

if messages is not None:
all_messages += messages

# Prepare the messages to be sent to the API
messages = [{"role": "user", "content": text}]
all_messages.append({"role": "user", "content": text})

log(
f"Prompt:\n{text}\n\nFunctions:\n{json.dumps(functions, indent=4)}",
Expand All @@ -527,7 +537,7 @@ def function_completion(
# If there are function(s) to call
response = openai.ChatCompletion.create(
model=model,
messages=messages,
messages=all_messages,
functions=functions,
function_call=function_call,
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="easycompletion",
version='0.3.2',
version='0.3.3',
description="Easy text completion and function calling. Also includes useful utilities for counting tokens, composing prompts and trimming them to fit within the token limit.",
long_description=long_description, # added this line
long_description_content_type="text/markdown", # and this line
Expand Down

0 comments on commit 68c0fc0

Please sign in to comment.