BeTaLabs commited on
Commit
0ad2bdb
1 Parent(s): 113eb1c

Update llm_handler.py

Browse files
Files changed (1) hide show
  1. llm_handler.py +55 -39
llm_handler.py CHANGED
@@ -1,43 +1,59 @@
1
- from llama_cpp import Llama
2
- from llama_cpp_agent import LlamaCppAgent
3
- from llama_cpp_agent import MessagesFormatterType
4
- from llama_cpp_agent.providers import LlamaCppPythonProvider
5
-
6
- # Initialize the Llama model
7
- llama_model = Llama("Qwen2-500M-Instruct-GGUF/Qwen2-500M-Instruct-f32.gguf", n_batch=1024, n_threads=24, n_gpu_layers=33, n_ctx=4098, verbose=False)
8
-
9
- # Create the provider
10
- provider = LlamaCppPythonProvider(llama_model)
11
-
12
- # Create the agent
13
- agent = LlamaCppAgent(
14
- provider,
15
- system_prompt="You are a helpful assistant who's purpose is it to help users craft and edit datasets.",
16
- predefined_messages_formatter_type=MessagesFormatterType.CHATML,
17
- debug_output=True
18
- )
19
-
20
- # Set provider settings
21
- settings = provider.get_provider_default_settings()
22
- settings.max_tokens = 4098
23
- settings.stream = True
24
-
25
- def send_to_llm(provider, msg_list):
26
  try:
27
-
28
- full_message = "\n".join([f"{msg['role']}: {msg['content']}" for msg in msg_list])
29
-
30
-
31
- response = agent.get_chat_response(full_message, llm_sampling_settings=settings)
32
-
33
-
34
- if isinstance(response, str):
35
- return response, None
36
- elif hasattr(response, 'content'):
37
- return response.content, None
38
- else:
39
- return str(response), None
40
  except Exception as e:
41
- print(f"Error in send_to_llm: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  return f"Error: {str(e)}", None
43
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ from openai import OpenAI
4
+ from params import load_params
5
+
6
+ def get_client():
7
+ params = load_params()
8
+ if params['PROVIDER'] == 'local-model':
9
+ return OpenAI(api_key="local-model", base_url=params['BASE_URL'])
10
+ return None
11
+
12
+ def send_to_chatgpt(msg_list):
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  try:
14
+ client = get_client()
15
+ completion = client.chat.completions.create(
16
+ model="phi3:latest",
17
+ temperature=0.6,
18
+ messages=msg_list
19
+ )
20
+ chatgpt_response = completion.choices[0].message.content
21
+ chatgpt_usage = completion.usage
22
+ return chatgpt_response, chatgpt_usage
 
 
 
 
23
  except Exception as e:
24
+ print(f"Error in send_to_chatgpt: {str(e)}")
25
+ return f"Error: {str(e)}", None
26
+
27
+ def send_to_anything_llm(msg_list):
28
+ params = load_params()
29
+ url = f"{params['BASE_URL']}/api/v1/workspace/{params['WORKSPACE']}/chat"
30
+ headers = {
31
+ 'accept': 'application/json',
32
+ 'Authorization': f"Bearer {params['API_KEY']}",
33
+ 'Content-Type': 'application/json'
34
+ }
35
+ message_content = " ".join(msg["content"] for msg in msg_list if "content" in msg)
36
+ data = {
37
+ "message": message_content,
38
+ "mode": "chat"
39
+ }
40
+ data_json = json.dumps(data)
41
+ try:
42
+ response = requests.post(url, headers=headers, data=data_json)
43
+ response.raise_for_status()
44
+ response_data = response.json()
45
+ chatgpt_response = response_data.get("textResponse")
46
+ chatgpt_usage = response_data.get("usage", {})
47
+ return chatgpt_response, chatgpt_usage
48
+ except requests.RequestException as e:
49
+ print(f"Error in send_to_anything_llm: {str(e)}")
50
  return f"Error: {str(e)}", None
51
 
52
+ def send_to_llm(msg_list):
53
+ params = load_params()
54
+ if params['PROVIDER'] == "local-model":
55
+ return send_to_chatgpt(msg_list)
56
+ elif params['PROVIDER'] == "anything-llm":
57
+ return send_to_anything_llm(msg_list)
58
+ else:
59
+ raise ValueError(f"Unknown provider: {params['PROVIDER']}")