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

Streaming input! #136

Merged
merged 4 commits into from
Feb 28, 2024
Merged

Streaming input! #136

merged 4 commits into from
Feb 28, 2024

Conversation

rgbkrk
Copy link
Owner

@rgbkrk rgbkrk commented Feb 28, 2024

This adds a new decorator @incremental_display that lets ChatLab know what you'd like to render streaming arguments with.

Export-1709079339234

import warnings
from graphviz import Digraph
from pydantic import BaseModel, Field
from typing import List
from chatlab import Chat, system
from chatlab.decorators import incremental_display

class Node(BaseModel):
    id: int
    label: str
    color: str

class Edge(BaseModel):
    source: int
    target: int
    label: str
    color: str = Field("black", description="A valid Graphviz color name or hex code.")

class KnowledgeGraph(BaseModel):
    nodes: List[Node] = Field(..., default_factory=list)
    edges: List[Edge] = Field(..., default_factory=list)


def visualize_knowledge_graph(kg: KnowledgeGraph, comment: str = "Knowledge Graph"):
    """Visualizes a knowledge graph using graphviz."""
    if kg is None:
        return

    dot = Digraph(comment=comment)

    for node in kg.nodes:
        dot.node(str(node.id), node.label, color=node.color)

    for edge in kg.edges:
        dot.edge(str(edge.source), str(edge.target), label=edge.label, color=edge.color)

    return dot


@incremental_display(visualize_knowledge_graph)
def store_knowledge_graph(kg: KnowledgeGraph, comment: str):
    """Creates a graphviz diagram for the user and stores it in their database."""
    return "Stored graph, visual shown"

chat = Chat(
    model="gpt-4-turbo-preview",
    chat_functions=[store_knowledge_graph],
)

await chat("Visually teach me about Ice Cream", temperature=0.7)

@rgbkrk rgbkrk merged commit bb4f382 into main Feb 28, 2024
7 checks passed
@rgbkrk rgbkrk deleted the streaming-input branch February 28, 2024 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant