Skip to content

Commit

Permalink
feat: stream control & optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nagarajpandith committed Dec 9, 2023
1 parent d8c1040 commit 48e1120
Show file tree
Hide file tree
Showing 2 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 @@ -64,12 +64,14 @@ curl -X POST -H "Content-Type: application/json" -d '{"content": "YOUR_PROMPT",

> (Last Tested on 9-12-2023)
> To test it yourself, run `python3 test.py` script.
> [!IMPORTANT]
> If a Provider isn't working, it's probably because it needs special args like auth='cookie' or 'jwt' or the WebDriver fails to connect, as web scraping is needed for most of the providers here or IP address blocking etc. Hence, do not consider the below results as final source of truth. To test it yourself, run `python3 test.py` script.
> [!NOTE]
> To know the Providers and their Models refer [this](https://github.com/xtekky/gpt4free?tab=readme-ov-file#-providers-and-models).
**Status Values**

- **Both:** The provider works successfully on both the local and hosted API.
- **Local:** The provider works successfully only on the local API.
- **Hosted:** The provider works successfully only on the hosted API.
Expand Down
16 changes: 13 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ def generate_response(model, llm, content):
def chat_completion():
data = request.get_json()

required_params = {'content', 'provider', 'api_key'}
required_params = {'content', 'api_key'}
missing_params = required_params - set(data.keys())

if missing_params:
error_msg = f"Missing parameters: {', '.join(missing_params)}"
return jsonify({"error": error_msg}), 400

content = data['content']
pname = data['provider']
pname = data.get('provider', 'You') # Use 'You' by default, if not provided
api_key = data['api_key']
stream = data.get('stream', True) # Use True by default, if not provided

if not api_key == API_KEY:
return jsonify({"error": "Invalid API key"}), 401
Expand All @@ -50,7 +51,16 @@ def chat_completion():
if not llm.supports_gpt_35_turbo:
model = "gpt-4"

return Response(generate_response(model, llm, content), content_type='text/event-stream', mimetype='text/event-stream')
if stream:
return Response(generate_response(model, llm, content), content_type='text/event-stream', mimetype='text/event-stream')
else:
response = g4f.ChatCompletion.create(
model=model,
provider=llm,
messages=[{"role": "user", "content": content}],
)

return jsonify(response)

@app.route('/working_providers', methods=['GET'])
def working_providers():
Expand Down

0 comments on commit 48e1120

Please sign in to comment.