Skip to content

Commit

Permalink
minor refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesfrye committed Mar 20, 2024
1 parent 8195ea1 commit 683ab10
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
20 changes: 10 additions & 10 deletions 06_gpu_and_ml/langchains/codelangchain/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import agent
import modal
from agent import stub
from agent import nodes, stub
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

Expand Down Expand Up @@ -28,20 +28,20 @@ def serve():
from langchain_core.runnables import RunnableLambda
from langserve import add_routes

def inp(question: str):
def inp(question: str) -> dict:
return {"keys": {"question": question, "iterations": 0}}

def out(state: dict) -> str:
print(state)
if "generate" in state: # patch issue with langserve playground
return "\n".join(
str(elem) for elem in state["generate"]["keys"]["generation"]
)
return state["keys"]["response"]
if "keys" in state:
return state["keys"]["response"]
elif "generate" in state:
return nodes.extract_response(state["generate"])
else:
return str(state)

runnable = agent.construct_graph(debug=False).compile()
graph = agent.construct_graph(debug=False).compile()

chain = RunnableLambda(inp) | runnable | RunnableLambda(out)
chain = RunnableLambda(inp) | graph | RunnableLambda(out)

add_routes(
web_app,
Expand Down
2 changes: 1 addition & 1 deletion 06_gpu_and_ml/langchains/codelangchain/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import modal

image = modal.Image.debian_slim(python_version="3.11").pip_install(
"beautifulsoup4~=4.12.3", # TODO: move to a "retrieval_image"
"beautifulsoup4~=4.12.3",
"langchain==0.1.11",
"langgraph==0.0.26",
"langchain_community==0.0.27",
Expand Down
1 change: 0 additions & 1 deletion 06_gpu_and_ml/langchains/codelangchain/edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def decide_to_finish(state: GraphState) -> str:
iter = state_dict["iterations"]

if error == "None" or iter >= 3:
# give up :<
print("---DECISION: FINISH---")
return "finish"
else:
Expand Down
27 changes: 21 additions & 6 deletions 06_gpu_and_ml/langchains/codelangchain/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,27 @@ def finish(self, state: GraphState) -> dict:
"""

print("---FINISHING---")
state_dict = state["keys"]
code_solution = state_dict["generation"]
prefix = code_solution[0].prefix
imports = code_solution[0].imports
code = code_solution[0].code

response = "\n".join([prefix, imports, code])
response = extract_response(state)

return {"keys": {"response": response}}


def extract_response(state: GraphState) -> str:
"""
Extract the response from the graph state
Args:
state (dict): The current graph state
Returns:
str: The response
"""

state_dict = state["keys"]
code_solution = state_dict["generation"][0]
prefix = code_solution.prefix
imports = code_solution.imports
code = code_solution.code

return "\n".join([prefix, imports, code])

0 comments on commit 683ab10

Please sign in to comment.