Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use system_prompt in solver configuration (breaking change) #19

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Spoken answers api with OpenAI or [LocalAI](https://github.com/go-skynet/LocalAI
from ovos_solver_openai_persona import OpenAIPersonaSolver

bot = OpenAIPersonaSolver({"key": "sk-XXX",
"persona": "helpful, creative, clever, and very friendly"})
"system_prompt": "helpful, creative, clever, and very friendly"})
print(bot.get_spoken_answer("describe quantum mechanics in simple terms"))
# Quantum mechanics is a branch of physics that deals with the behavior of particles on a very small scale, such as atoms and subatomic particles. It explores the idea that particles can exist in multiple states at once and that their behavior is not predictable in the traditional sense.
print(bot.spoken_answer("Quem encontrou o caminho maritimo para o Brazil", {"lang": "pt-pt"}))
Expand All @@ -33,7 +33,7 @@ This plugin can be configured to use a LocalAI server instead of OpenAI.
"key": <your_OpenAI_key>,
"enable_memory": true,
"memory_size": 15,
"initial_prompt": "You are a helpful assistant."
"system_prompt": "You are a helpful assistant."
}
```

Expand Down
16 changes: 9 additions & 7 deletions ovos_solver_openai_persona/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Iterable, List, Dict

from ovos_solver_openai_persona.engines import OpenAIChatCompletionsSolver

Expand All @@ -9,14 +9,10 @@ class OpenAIPersonaSolver(OpenAIChatCompletionsSolver):
def __init__(self, config=None):
# defaults to gpt-3.5-turbo
super().__init__(config=config)
self.default_persona = config.get("persona") or "helpful, creative, clever, and very friendly."
self.system_prompt = config.get("system_prompt") or "You are a helpful assistant. You are creative, clever, and very friendly."

def get_chat_history(self, persona=None):
persona = persona or self.default_persona
initial_prompt = f"You are a helpful assistant. " \
f"You give short and factual answers. " \
f"You are {persona}"
return super().get_chat_history(initial_prompt)
return super().get_chat_history(self.system_prompt)

# officially exported Solver methods
def get_spoken_answer(self, query: str,
Expand All @@ -38,6 +34,12 @@ def get_spoken_answer(self, query: str,
return None
return answer

def stream_chat_utterances(self, messages: List[Dict[str, str]],
lang: Optional[str] = None,
units: Optional[str] = None) -> Iterable[str]:
messages = [{"role": "system", "content": self.system_prompt }] + messages
answer = super().stream_chat_utterances(messages, lang, units)
yield from answer

# for ovos-persona
LLAMA_DEMO = {
Expand Down
13 changes: 6 additions & 7 deletions ovos_solver_openai_persona/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, config=None):
self.memory = config.get("enable_memory", True)
self.max_utts = config.get("memory_size", 15)
self.qa_pairs = [] # tuple of q+a
self.initial_prompt = config.get("initial_prompt", "You are a helpful assistant.")
self.system_prompt = config.get("system_prompt", "You are a helpful assistant.")

# OpenAI API integration
def _do_api_request(self, messages):
Expand Down Expand Up @@ -163,19 +163,19 @@ def _do_streaming_api_request(self, messages):
continue
yield chunk["choices"][0]["delta"]["content"]

def get_chat_history(self, initial_prompt=None):
def get_chat_history(self, system_prompt=None):
qa = self.qa_pairs[-1 * self.max_utts:]
initial_prompt = initial_prompt or self.initial_prompt or "You are a helpful assistant."
system_prompt = system_prompt or self.system_prompt or "You are a helpful assistant."
messages = [
{"role": "system", "content": initial_prompt},
{"role": "system", "content": system_prompt},
]
for q, a in qa:
messages.append({"role": "user", "content": q})
messages.append({"role": "assistant", "content": a})
return messages

def get_messages(self, utt, initial_prompt=None) -> MessageList:
messages = self.get_chat_history(initial_prompt)
def get_messages(self, utt, system_prompt=None) -> MessageList:
messages = self.get_chat_history(system_prompt)
messages.append({"role": "user", "content": utt})
return messages

Expand Down Expand Up @@ -267,4 +267,3 @@ def get_spoken_answer(self, query: str,
messages = self.get_messages(query)
# just for api compat since it's a subclass, shouldn't be directly used
return self.continue_chat(messages=messages, lang=lang, units=units)

6 changes: 3 additions & 3 deletions ovos_solver_openai_persona/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# START_VERSION_BLOCK
VERSION_MAJOR = 1
VERSION_MINOR = 1
VERSION_BUILD = 2
VERSION_MAJOR = 2
VERSION_MINOR = 0
VERSION_BUILD = 0
VERSION_ALPHA = 0
# END_VERSION_BLOCK
Loading