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

Add a way to stream output to a UI via sockets #1367

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion autogen/oai/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import annotations

import os
import sys
from typing import Any, List, Optional, Dict, Callable, Tuple, Union
Expand All @@ -16,6 +15,8 @@
from autogen.token_count_utils import count_token
from autogen._pydantic import model_dump

import socket_connection_singleton

TOOL_ENABLED = False
try:
import openai
Expand Down Expand Up @@ -488,6 +489,8 @@ def _completions_create(self, client: OpenAI, params: Dict[str, Any]) -> ChatCom

# If content is present, print it to the terminal and update response variables
if content is not None:
if socket_connection_singleton.clientsocket is not None:
socket_connection_singleton.clientsocket.send(bytes(str(content), "utf-8"))
print(content, end="", flush=True)
response_contents[choice.index] += content
completion_tokens += 1
Expand Down
30 changes: 30 additions & 0 deletions autogen/oai/socket_connection_singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#This is a singleton. You use it to pass variables around from script to script, on any level.
#Import this script into your top script and define your socket connection, then import this script's variables
#into the file where you want to use them.

#For example:

#main.py:
#import socket
#import socket_connection_singleton

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.bind((socket.gethostname(), 1235))
#s.listen(5)
#logging.info("Waiting for connection...")
#clientsocket, address = s.accept()
#socket_connection_singleton.s = s
#socket_connection_singleton.clientsocket = clientsocket


#client.py:

#import socket
#import socket_connection_singleton

#clientsocket = socket_connection_singleton.clientsocket
#clientsocket.send(bytes("Hello, World!", "utf-8"))

s = None
clientsocket = None
address = None
Loading