File size: 2,789 Bytes
d56ba03
 
 
 
 
 
 
 
 
 
 
 
 
94de721
 
d56ba03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
from functools import lru_cache

import streamlit as st
import anthropic
import openai


# st.set_page_config(layout="wide")


class AnthropicSerivce:
    def __init__(self):
        if "ANTHROPIC_API_KEY" not in os.environ:
            os.environ["ANTHROPIC_API_KEY"] = st.text_input("Anthropic API Key", type="password")
        self.client = self.anthropic_client()

    @staticmethod
    @lru_cache
    def anthropic_client():
        return anthropic.Client(os.environ["ANTHROPIC_API_KEY"])

    def prompt(self, prompt, max_tokens_to_sample: int = 2000):
        response = self.client.completion_stream(
            prompt=f"{anthropic.HUMAN_PROMPT} {prompt}{anthropic.AI_PROMPT}",
            stop_sequences=[anthropic.HUMAN_PROMPT],
            max_tokens_to_sample=max_tokens_to_sample,
            model="claude-v1",
            stream=True,
        )
        return response


class OpenAIService:
    def __init__(self):
        openai.api_key = os.environ["OPENAI_API_KEY"]

    def list_models(self):
        return openai.Model.list()

    def prompt(self, prompt):
        # return openai.Completion.create(model="ada", prompt=prompt)
        return openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
            # model="gpt-4", messages=[{"role": "user", "content": prompt}]
        )


def app():
    prompt = st.text_area("Prompt", value="How many toes do dogs have?", height=100)
    tabs = st.tabs(["Anthropic", "OpenAI"])
    if prompt:
        # -- Antrhopic
        with tabs[0]:
            anthropic_service = AnthropicSerivce()
            response = anthropic_service.prompt(prompt=prompt)
            # for data in response:
            #     col = st.columns(2)
            #     with col[0]:
            #         st.markdown("---")
            #         st.write(data)
            #     with col[1]:
            #         st.markdown("---")
            #         st.markdown(data["completion"])
            # -- pick the last response

            col = st.columns(2)
            answer = list(response)[-1]
            with col[0]:
                st.write(answer)
            with col[1]:
                st.code(answer["completion"])

        # -- OpenAI
        with tabs[1]:
            openai_service = OpenAIService()
            response = openai_service.prompt(prompt=prompt)

            col = st.columns(2)
            # st.write(openai_service.list_models())
            with col[0]:
                st.write(response)
                st.markdown("---")
            with col[1]:
                answer = response.choices[0]["message"]["content"]
                st.code(answer, language="python")
                st.markdown("---")


if __name__ == "__main__":

    app()