File size: 2,790 Bytes
64a1b61
 
 
 
 
 
69d5fa8
64a1b61
 
 
 
4a00c6f
5794f93
 
64a1b61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c89f40c
 
 
 
 
 
5794f93
 
f0d723e
5794f93
 
 
 
 
 
 
 
 
 
 
64a1b61
5794f93
64a1b61
 
 
c89f40c
69d5fa8
64a1b61
 
 
 
 
 
69d5fa8
64a1b61
 
c89f40c
 
 
64a1b61
 
 
 
c89f40c
64a1b61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from gradio_client import Client
from trulens_eval.schema import Select
from trulens_eval.tru import Tru
from trulens_eval.feedback import Feedback
from trulens_eval.feedback import OpenAI as Feedback_OpenAI
from langchain.llms import HuggingFacePipeline
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
import os

# from langchain.llms import Runnable


# Access environment variables
openai_api_key = os.environ.get("OPENAI_API_KEY")
huggingface_api_token = os.environ.get("HUGGINGFACE_API_TOKEN")


# Define a feedback function for query-statement relevance using OpenAI
feedback_openai = Feedback_OpenAI()
qa_relevance = Feedback(feedback_openai.relevance, name="Answer Relevance").on_input_output()

# Create a Tru object
tru = Tru()

# Set the window memory to go back 4 turns
window_memory = ConversationBufferWindowMemory(k=4)

# Define a custom function to interact with the Gradio client
def gradio_client_interaction(prompt):
    client = Client("https://tonic-stablemed-chat.hf.space/")
    result = client.predict(prompt, prompt, api_name="/predict")
    return result['data'][0]  # Assuming the response format

class GradioLLM(Runnable):
    def __init__(self, client_url):
        super().__init__()  # Call the constructor of Runnable if necessary
        self.client = Client(client_url)

    def generate(self, prompt, **kwargs):
        # Assuming the API expects 'prompt' and 'system_prompt' as inputs
        result = self.client.predict(prompt, prompt, api_name="/predict")
        return result['data'][0]  # Adjust based on the actual response format

# Initialize the GradioLLM with the URL
gradio_llm = GradioLLM("https://tonic-stablemed-chat.hf.space/")

# Create the ConversationChain with the GradioLLM
conversation = ConversationChain(
    llm=gradio_llm,
    verbose=True,
    memory=window_memory
)
# Update the conversation prompt template
conversation.prompt.template = '''The following is a consult between a clinical consultant and a public health and medical expert. The AI is an expert on medicine and public health and gives recommendations specific to location and conditions. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
{history}
Human: {input}
AI:'''

# Wrap the conversation with TruChain to instrument it
tc_conversation = tru.Chain(conversation, app_id='Trulens-StableMed', feedbacks=[qa_relevance])

# Make a prediction using the wrapped conversation
user_input = "Howdy!"
system_prompt = "Howdy!"
result = tc_conversation.generate(user_input, system_prompt=system_prompt)

# Print the result
print(result)

# Run the TruLens dashboard
tru.run_dashboard()