-
-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update docs/. etc/. g4f/. README.md (#2515)
Co-authored-by: kqlio67 <>
- Loading branch information
Showing
28 changed files
with
1,233 additions
and
289 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
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
6 changes: 4 additions & 2 deletions
6
etc/examples/image_api.py → etc/examples/api_generations_image.py
100644 → 100755
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,9 +1,11 @@ | ||
import requests | ||
url = "http://localhost:1337/v1/images/generations" | ||
body = { | ||
"model": "dall-e", | ||
"model": "flux", | ||
"prompt": "hello world user", | ||
"response_format": None, | ||
#"response_format": "url", | ||
#"response_format": "b64_json", | ||
} | ||
data = requests.post(url, json=body, stream=True).json() | ||
print(data) | ||
print(data) |
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,33 @@ | ||
from g4f.client import Client | ||
|
||
class ConversationHandler: | ||
def __init__(self, model="gpt-4"): | ||
self.client = Client() | ||
self.model = model | ||
self.conversation_history = [] | ||
|
||
def add_user_message(self, content): | ||
self.conversation_history.append({ | ||
"role": "user", | ||
"content": content | ||
}) | ||
|
||
def get_response(self): | ||
response = self.client.chat.completions.create( | ||
model=self.model, | ||
messages=self.conversation_history | ||
) | ||
assistant_message = { | ||
"role": response.choices[0].message.role, | ||
"content": response.choices[0].message.content | ||
} | ||
self.conversation_history.append(assistant_message) | ||
return assistant_message["content"] | ||
|
||
# Usage example | ||
conversation = ConversationHandler() | ||
conversation.add_user_message("Hello!") | ||
print("Assistant:", conversation.get_response()) | ||
|
||
conversation.add_user_message("How are you?") | ||
print("Assistant:", conversation.get_response()) |
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,25 @@ | ||
import asyncio | ||
from g4f.client import AsyncClient | ||
|
||
async def main(): | ||
client = AsyncClient() | ||
|
||
stream = client.chat.completions.create( | ||
model="gpt-4", | ||
messages=[{"role": "user", "content": "Say hello there!"}], | ||
stream=True, | ||
) | ||
|
||
accumulated_text = "" | ||
try: | ||
async for chunk in stream: | ||
if chunk.choices and chunk.choices[0].delta.content: | ||
content = chunk.choices[0].delta.content | ||
accumulated_text += content | ||
print(content, end="", flush=True) | ||
except Exception as e: | ||
print(f"\nError occurred: {e}") | ||
finally: | ||
print("\n\nFinal accumulated text:", accumulated_text) | ||
|
||
asyncio.run(main()) |
Empty file.
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,17 @@ | ||
import asyncio | ||
from g4f.client import AsyncClient | ||
|
||
async def main(): | ||
client = AsyncClient() | ||
|
||
response = await client.chat.completions.create( | ||
model="gpt-4o", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "how does a court case get to the Supreme Court?"} | ||
] | ||
) | ||
|
||
print(response.choices[0].message.content) | ||
|
||
asyncio.run(main()) |
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,13 @@ | ||
from g4f.client import Client | ||
|
||
client = Client() | ||
|
||
response = client.chat.completions.create( | ||
model="gpt-4o", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "how does a court case get to the Supreme Court?"} | ||
], | ||
) | ||
|
||
print(response.choices[0].message.content) |
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,49 @@ | ||
import asyncio | ||
from g4f.client import Client, AsyncClient | ||
|
||
question = """ | ||
Hey! How can I recursively list all files in a directory in Python? | ||
""" | ||
|
||
# Synchronous streaming function | ||
def sync_stream(): | ||
client = Client() | ||
stream = client.chat.completions.create( | ||
model="gpt-4", | ||
messages=[ | ||
{"role": "user", "content": question} | ||
], | ||
stream=True, | ||
) | ||
|
||
for chunk in stream: | ||
if chunk.choices[0].delta.content: | ||
print(chunk.choices[0].delta.content or "", end="") | ||
|
||
# Asynchronous streaming function | ||
async def async_stream(): | ||
client = AsyncClient() | ||
stream = client.chat.completions.create( | ||
model="gpt-4", | ||
messages=[ | ||
{"role": "user", "content": question} | ||
], | ||
stream=True, | ||
) | ||
|
||
async for chunk in stream: | ||
if chunk.choices and chunk.choices[0].delta.content: | ||
print(chunk.choices[0].delta.content, end="") | ||
|
||
# Main function to run both streams | ||
def main(): | ||
print("Synchronous Stream:") | ||
sync_stream() | ||
print("\n\nAsynchronous Stream:") | ||
asyncio.run(async_stream()) | ||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except Exception as e: | ||
print(f"An error occurred: {str(e)}") |
Oops, something went wrong.