Skip to content

Commit 6952d49

Browse files
authored
Merge pull request #215 from NimbleBoxAI/fury
[chore] add worker architecture
2 parents 88cd04e + 2dd79eb commit 6952d49

File tree

9 files changed

+190
-94
lines changed

9 files changed

+190
-94
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ ae e0 a5 87 e0 a4 b5 20 e0 a4 9c e0 a4 af
2424

2525
The documentation page contains all the information on using `chainfury` and `chainfury_server`.
2626

27+
#### `chainfury`
28+
29+
<img src="https://d2e931syjhr5o9.cloudfront.net/tune-research/assets/cf_arch.png" align="center"/>
30+
31+
#### `chainfury_server`
32+
33+
<img src="https://d2e931syjhr5o9.cloudfront.net/tune-research/assets/cfs_arch.png" align="center"/>
34+
2735
# Looking for Inspirations?
2836

2937
Here's a few example to get your journey started on Software 2.0:
@@ -86,7 +94,7 @@ source venv/bin/activate
8694
You will need to have `yarn` installed to build the frontend and move it to the correct location on the server
8795

8896
```bash
89-
sh stories/build_and_copy.sh
97+
sh build_ui.sh
9098
```
9199

92100
Once the static files are copied we can now proceed to install dependecies:
@@ -104,7 +112,7 @@ You can now visit [localhost:8000](http://localhost:8000/ui/) to see the GUI and
104112
There are a few test cases for super hard problems like `get_kv` which checks the `chainfury.base.get_value_by_keys` function.
105113

106114
```bash
107-
python3 -m tests -v
115+
python3 tests/main.py
108116
```
109117

110118
# Contibutions

scripts/build_and_copy.sh renamed to build_ui.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ cd client
1414
yarn install
1515
yarn build
1616

17-
# Go back to the root directory
18-
cd ..
19-
2017
# copy the dist folder to the server
2118
# Go into the server folder, remove the old static folder and copy the new dist folder, copy index.html to templates
2219
echo "Copying the generated files to the server"

chainfury/cli.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313
from chainfury import Chain
1414
from chainfury.version import __version__
15-
from chainfury.components import all_items
16-
from chainfury.core import model_registry, programatic_actions_registry, memory_registry
17-
from chainfury.chat import Chat, Message
15+
from chainfury.core import model_registry
16+
from chainfury.types import Thread, Message
1817

1918

2019
class CLI:
@@ -115,7 +114,7 @@ def sh(
115114
cf_model.set_api_token(token)
116115

117116
# loop for user input through command line
118-
chat = Chat()
117+
thread = Thread()
119118
usr_cntr = 0
120119
while True:
121120
try:
@@ -126,21 +125,21 @@ def sh(
126125
break
127126
if user_input == "exit" or user_input == "quit" or user_input == "":
128127
break
129-
chat.add(Message(user_input, Message.HUMAN))
128+
thread.add(Message(user_input, Message.HUMAN))
130129

131130
print(f"\033[1m\033[34m ASSISTANT \033[39m:\033[0m ", end="", flush=True)
132131
if stream:
133132
response = ""
134-
for str_token in cf_model.stream_chat(chat, model=model):
133+
for str_token in cf_model.stream_chat(thread, model=model):
135134
response += str_token
136135
print(str_token, end="", flush=True)
137136
print() # new line
138-
chat.add(Message(response, Message.GPT))
137+
thread.add(Message(response, Message.GPT))
139138
else:
140-
response = cf_model.chat(chat, model=model)
139+
response = cf_model.chat(thread, model=model)
141140
print(response)
142141

143-
chat.add(Message(response, Message.GPT))
142+
thread.add(Message(response, Message.GPT))
144143
usr_cntr += 1
145144

146145

chainfury/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ def get_now_float() -> float: # type: ignore
418418
"""Get the current datetime in UTC timezone as a float"""
419419
return SimplerTimes.get_now_datetime().timestamp()
420420

421+
def get_now_fp64() -> float: # type: ignore
422+
"""Get the current datetime in UTC timezone as a float"""
423+
return SimplerTimes.get_now_datetime().timestamp()
424+
421425
def get_now_i64() -> int: # type: ignore
422426
"""Get the current datetime in UTC timezone as a int"""
423427
return int(SimplerTimes.get_now_datetime().timestamp())

scripts/list_builtins.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

server/chainfury_server/api/chains.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def create_chain(
3131
return T.ApiResponse(message="Name not specified")
3232
if chatbot_data.dag:
3333
for n in chatbot_data.dag.nodes:
34-
if len(n.id) > Env.CFS_MAXLEN_CF_NDOE():
34+
if len(n.id) > Env.CFS_MAXLEN_CF_NODE():
3535
raise HTTPException(
3636
status_code=400,
37-
detail=f"Node ID length cannot be more than {Env.CFS_MAXLEN_CF_NDOE()}",
37+
detail=f"Node ID length cannot be more than {Env.CFS_MAXLEN_CF_NODE()}",
3838
)
3939

4040
# DB call
@@ -245,16 +245,16 @@ def run_chain(
245245

246246
if as_task:
247247
# when run as a task this will return a task ID that will be submitted
248-
raise HTTPException(501, detail="Not implemented yet")
249-
# result = engine.submit(
250-
# chatbot=chatbot,
251-
# prompt=prompt,
252-
# db=db,
253-
# start=time.time(),
254-
# store_ir=store_ir,
255-
# store_io=store_io,
256-
# )
257-
# return result
248+
# raise HTTPException(501, detail="Not implemented yet")
249+
result = engine.submit(
250+
chatbot=chatbot,
251+
prompt=prompt,
252+
db=db,
253+
start=time.time(),
254+
store_ir=store_ir,
255+
store_io=store_io,
256+
)
257+
return result
258258
elif stream:
259259

260260
def _get_streaming_response(result):

server/chainfury_server/database.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from dataclasses import dataclass, asdict
1111
from typing import Dict, Any
1212

13-
from sqlalchemy.pool import QueuePool
13+
from sqlalchemy.pool import QueuePool, NullPool
1414
from sqlalchemy.exc import IntegrityError
1515
from sqlalchemy.ext.declarative import declarative_base
1616
from sqlalchemy.orm import Session, scoped_session, sessionmaker, relationship
@@ -55,6 +55,8 @@
5555
)
5656
else:
5757
logger.info(f"Using via database URL")
58+
# https://stackoverflow.com/a/73764136
59+
#
5860
engine = create_engine(
5961
db,
6062
poolclass=QueuePool,
@@ -84,7 +86,7 @@ def get_random_number(length) -> int:
8486
return random_numbers
8587

8688

87-
def get_local_session() -> sessionmaker:
89+
def get_local_session(engine) -> sessionmaker:
8890
logger.debug("Database opened successfully")
8991
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
9092
return SessionLocal
@@ -101,7 +103,7 @@ def db_session() -> Session: # type: ignore
101103

102104

103105
def fastapi_db_session():
104-
sess_cls = get_local_session()
106+
sess_cls = get_local_session(engine)
105107
db = sess_cls()
106108
try:
107109
yield db
@@ -272,7 +274,7 @@ class Prompt(Base):
272274
meta: Dict[str, Any] = Column(JSON)
273275

274276
# migrate to snowflake ID
275-
sf_id = Column(String(19), nullable=True)
277+
# sf_id = Column(String(19), nullable=True)
276278

277279
def to_dict(self):
278280
return {
@@ -303,7 +305,7 @@ class ChainLog(Base):
303305
)
304306
created_at: datetime = Column(DateTime, nullable=False)
305307
prompt_id: int = Column(Integer, ForeignKey("prompt.id"), nullable=False)
306-
node_id: str = Column(String(Env.CFS_MAXLEN_CF_NDOE()), nullable=False)
308+
node_id: str = Column(String(Env.CFS_MAXLEN_CF_NODE()), nullable=False)
307309
worker_id: str = Column(String(Env.CFS_MAXLEN_WORKER()), nullable=False)
308310
message: str = Column(Text, nullable=False)
309311
data: Dict[str, Any] = Column(JSON, nullable=True)

0 commit comments

Comments
 (0)