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

feat: GPT35Generator #5714

Merged
merged 44 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a243cae
chatgpt backend
ZanSara Sep 4, 2023
f59abe8
fix tests
ZanSara Sep 4, 2023
5f70a65
reno
ZanSara Sep 4, 2023
ffb1a8f
remove print
ZanSara Sep 4, 2023
853f29d
helpers tests
ZanSara Sep 4, 2023
f0c5a8d
add chatgpt generator
ZanSara Sep 4, 2023
0a25414
use openai sdk
ZanSara Sep 4, 2023
5105ae8
remove backend
ZanSara Sep 4, 2023
7d0c8e6
tests are broken
ZanSara Sep 4, 2023
de46d10
fix tests
ZanSara Sep 5, 2023
28d83f4
stray param
ZanSara Sep 5, 2023
30b4bc3
move _check_troncated_answers into the class
ZanSara Sep 5, 2023
1b744e4
wrong import
ZanSara Sep 5, 2023
ab0e45c
rename function
ZanSara Sep 5, 2023
fc7dc05
typo in test
ZanSara Sep 5, 2023
3e43dcd
add openai deps
ZanSara Sep 5, 2023
c3381e3
mypy
ZanSara Sep 5, 2023
a204d14
Merge branch 'main' into chatgpt-llm-generator
ZanSara Sep 5, 2023
8d6f134
improve system prompt docstring
ZanSara Sep 5, 2023
8e0c1c6
Merge branch 'chatgpt-llm-generator' of github.com:deepset-ai/haystac…
ZanSara Sep 5, 2023
e1652f8
typos update
dfokina Sep 5, 2023
2a256b2
Update haystack/preview/components/generators/openai/chatgpt.py
ZanSara Sep 5, 2023
7178f23
pylint
ZanSara Sep 5, 2023
9eb7900
Merge branch 'chatgpt-llm-generator' of github.com:deepset-ai/haystac…
ZanSara Sep 5, 2023
13104de
Merge branch 'main' into chatgpt-llm-generator
ZanSara Sep 5, 2023
155485f
Update haystack/preview/components/generators/openai/chatgpt.py
ZanSara Sep 5, 2023
b2187c3
Update haystack/preview/components/generators/openai/chatgpt.py
ZanSara Sep 5, 2023
ed08e34
Update haystack/preview/components/generators/openai/chatgpt.py
ZanSara Sep 5, 2023
cc0bb7d
review feedback
ZanSara Sep 5, 2023
c58ab26
fix tests
ZanSara Sep 5, 2023
835fd0c
freview feedback
ZanSara Sep 5, 2023
0eb43f9
reno
ZanSara Sep 5, 2023
e8d92dd
remove tenacity mock
ZanSara Sep 6, 2023
0aeb875
gpt35generator
ZanSara Sep 6, 2023
9167e05
fix naming
ZanSara Sep 6, 2023
941cc66
remove stray references to chatgpt
ZanSara Sep 6, 2023
04ec229
fix e2e
ZanSara Sep 6, 2023
4eece1e
Merge branch 'main' into chatgpt-llm-generator
ZanSara Sep 6, 2023
8fb06ae
Update releasenotes/notes/chatgpt-llm-generator-d043532654efe684.yaml
ZanSara Sep 6, 2023
46385ac
add another test
ZanSara Sep 6, 2023
812e8b9
Merge branch 'main' into chatgpt-llm-generator
ZanSara Sep 6, 2023
3ca3f73
test wrong model name
ZanSara Sep 6, 2023
1015424
review feedback
ZanSara Sep 6, 2023
b79c7c1
Merge branch 'main' into chatgpt-llm-generator
ZanSara Sep 6, 2023
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
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
ZanSara committed Sep 5, 2023
commit de46d104220c1f9ad627be92987e4937442a18a9
29 changes: 19 additions & 10 deletions haystack/preview/components/generators/openai/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
def default_streaming_callback(chunk: Dict[str, Any]) -> Dict[str, Any]:
"""
Default callback function for streaming responses from OpenAI API.
Prints the tokens to stdout as soon as they are received and returns the chunk unchanged.
Prints the tokens of the first completion to stdout as soon as they are received and returns the chunk unchanged.
"""
if chunk.choices.delta.content:
print(chunk.choices.delta.content, flush=True, end="")
if hasattr(chunk.choices[0].delta, "content"):
print(chunk.choices[0].delta.content, flush=True, end="")
return chunk


Expand Down Expand Up @@ -80,6 +80,9 @@ def __init__(
values are the bias to add to that token.
- `openai_organization`: The OpenAI organization ID.
"""
if not api_key:
logger.warning("OpenAI API key is missing. You need to provide an API key to Pipeline.run().")

self.api_key = api_key
self.model_name = model_name
self.system_prompt = system_prompt
Expand Down Expand Up @@ -178,11 +181,14 @@ def run(
See OpenAI documentation](https://platform.openai.com/docs/api-reference/chat) for more details.
"""
api_key = api_key if api_key is not None else self.api_key
model_name = model_name if model_name is not None else self.model_name
if not api_key:
raise ValueError("OpenAI API key is missing. Please provide an API key.")

model_name = model_name or self.model_name
system_prompt = system_prompt if system_prompt is not None else self.system_prompt
model_parameters = model_parameters if model_parameters is not None else self.model_parameters
streaming_callback = streaming_callback if streaming_callback is not None else self.streaming_callback
api_base_url = api_base_url if api_base_url is not None else self.api_base_url
streaming_callback = streaming_callback or self.streaming_callback
api_base_url = api_base_url or self.api_base_url

if system_prompt:
system_message = ChatMessage(content=system_prompt, role="system")
Expand Down Expand Up @@ -215,9 +221,12 @@ def run(

if hasattr(choice.delta, "content"):
replies[choice.index] += choice.delta.content
metadata[choice.index].update(
{"model": chunk.model, "index": choice.index, "finish_reason": choice.finish_reason}
)
metadata[choice.index] = {
"model": chunk.model,
"index": choice.index,
"finish_reason": choice.finish_reason,
}

all_replies.append(list(replies.values()))
all_metadata.append(list(metadata.values()))
check_truncated_answers(list(metadata.values()))
Expand All @@ -228,7 +237,7 @@ def run(
"model": completion.model,
"index": choice.index,
"finish_reason": choice.finish_reason,
**completion.usage.__dict__,
"usage": dict(completion.usage.items()),
}
for choice in completion.choices
]
Expand Down
Loading